[.Net] Creation d’un disque virtuel

virtual drive

Aujourd’hui je vais vous partager un exemple de code trouvé en fouinant sur le net qui permet de créer un disque virtuel.

Les disques virtuels permettent tout simplement d’émuler un lecteur disque en spécifiant un chemin vers lequel il pointera, ce qui peut simplifier les recherches et les développement.

Les virtual drive permettent donc de faire rediriger une lettre d’accès vers un répertoire physique.

Il nous sera ainsi possible de faire pointer le répertoire “C:\Windows” vers “Z:\”.

La classe VirtualDrive

Source code    
Imports System.IO
Imports System.Runtime.InteropServices
 
''' <summary>
''' Creation and deletion of non-persistent virtual drives.
''' The drive has to be restored after a reboot of the system.
''' Does not appear in the registry key: HKLM\System\MountedDevices
''' 
''' VirtualDrive - © Konstantin Gross
''' </summary>
Public Class VirtualDrive
	#Region "Win32"
	<DllImport("kernel32.dll", CharSet := CharSet.Auto)> _
	Private Shared Function DefineDosDevice(dwFlags As Integer, lpDeviceName As String, lpTargetPath As String) As Boolean
	End Function
 
	<DllImport("kernel32.dll", CharSet := CharSet.Auto)> _
	Private Shared Function GetDriveType(lpRootPathName As String) As Integer
	End Function
 
	Private Const DDD_RAW_TARGET_PATH As Integer = &H1
	Private Const DDD_REMOVE_DEFINITION As Integer = &H2
	Private Const DDD_EXACT_MATCH_ON_REMOVE As Integer = &H4
 
	Private Const DRIVE_UNKNOWN As Integer = 0
	Private Const DRIVE_NO_ROOT_DIR As Integer = 1
	Private Const DRIVE_FIXED As Integer = 3
	#End Region
 
	#Region "Public methods"
 
	#Region "Create"
	''' <summary>
	''' Creation of a non-persistent drive.
	''' </summary>
	''' <param name="driveChar">Drive letter</param>
	''' <param name="path">Path to the folder to be linked</param>
	''' <returns>True/False on attempt to create the drive</returns>
	Public Shared Function Create(driveChar As Char, path As String) As Boolean
		Return DDDOperation(driveChar, path, True)
	End Function
	#End Region
 
	#Region "Delete"
	''' <summary>
	''' Deletion of a non-persistent drive pop over to this site.
	''' </summary>
	''' <param name="driveChar">Drive letter</param>
	''' <param name="path">Path to the folder to be linked</param>
	''' <returns>True/False on attempt to delete the drive</returns>
	Public Shared Function Delete(driveChar As Char, path As String) As Boolean
		Return DDDOperation(driveChar, path, False)
	End Function
	#End Region
 
	#End Region
 
	#Region "Private methods"
 
	#Region "DDDOperations"
	Private Shared Function DDDOperation(driveChar As Char, path__1 As String, create As Boolean) As Boolean
		'Valid directory?
		If Not Directory.Exists(path__1) Then
			Return False
		End If
 
		Dim drive As String = String.Format("{0}:", driveChar.ToString().ToUpper())
 
		'Does the volume exist?
		Dim type As Integer = GetDriveType(String.Format("{0}{1}", drive, Path.DirectorySeparatorChar))
 
		'Hint: Type of a created virtual drive is DRIVE_FIXED
		If (create AndAlso type <> DRIVE_UNKNOWN AndAlso type <> DRIVE_NO_ROOT_DIR) OrElse (Not create AndAlso type <> DRIVE_FIXED) Then
			Return False
		End If
 
		Dim flags As Integer = DDD_RAW_TARGET_PATH
 
		If Not create Then
			flags = flags Or (DDD_REMOVE_DEFINITION Or DDD_EXACT_MATCH_ON_REMOVE)
		End If
		Return DefineDosDevice(flags, drive, String.Format("{0}??{0}{1}", Path.DirectorySeparatorChar, path__1))
	End Function
	#End Region
 
	#End Region
End Class

 

Merci donc à Konstantin Gross pour cette superbe classe 🙂

One Response to [.Net] Creation d’un disque virtuel

  1. Mickael says:

    Sous Windows, on peut utiliser la commande subst, donc pour faire pointer “C:Windows” vers “Z:” il suffit de faire :
    subst c:windows z:

    ++

Leave a Reply

Your email address will not be published. Required fields are marked *