Imports System Imports System.IO Imports System.Runtime.InteropServices Public Class clsVirtualDrive 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 <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function DefineDosDevice(ByVal dwFlags As Integer, ByVal lpDeviceName As String, ByVal lpTargetPath As String) As Boolean End Function Private Const DRIVE_UNKNOWN As Integer = 0 Private Const DRIVE_NO_ROOT_DIR As Integer = 1 Private Const DRIVE_FIXED As Integer = 3 <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _ Private Shared Function GetDriveType(ByVal lpRootPathName As String) As Integer End Function Public Function Create(ByVal driveChar As Char, ByVal path As String) As Boolean Return DDDOperation(driveChar, path, True) End Function Public Function Delete(ByVal driveChar As Char, ByVal path As String) As Boolean Return DDDOperation(driveChar, path, False) End Function Private Function DDDOperation(ByVal driveChar As Char, ByVal path As String, ByVal create As Boolean) As Boolean 'répertoire valide ? If Not Directory.Exists(path) Then Return False End If Dim drive As String = String.Format("{0}:", driveChar.ToString().ToUpper()) 'le volume existe ? Dim type As Integer = GetDriveType(String.Format("{0}{1}", drive, IO.Path.DirectorySeparatorChar)) 'Nota : un lecteur virtuel est de type DRIVE_FIXED une fois créé. 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}", IO.Path.DirectorySeparatorChar, path)) End Function End Class
Option Strict On Public Class Form1 Dim vd As clsVirtualDrive Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load vd = New clsVirtualDrive If vd.Create("Y"c, Application.StartupPath) = True Then MessageBox.Show("lecteur monté") 'décryptage des fichiers du dossier à faire ici '... 'surveillance du dossier pour ajout, modif, suppression etc... Dim fs As New IO.FileSystemWatcher(Application.StartupPath) AddHandler fs.Created, AddressOf fichiercree AddHandler fs.Changed, AddressOf fichiermodif 'etc... fs.EnableRaisingEvents = True End If End Sub Private Sub fichiercree(ByVal sender As Object, ByVal e As IO.FileSystemEventArgs) 'cryptage du fichier créé ou ajouté ici End Sub Private Sub fichiermodif(ByVal sender As Object, ByVal e As IO.FileSystemEventArgs) 'cryptage du fichier modifié ici End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing vd.Delete("Y"c, Application.StartupPath) MessageBox.Show("lecteur démonté") 'cryptage des fichiers du dossier à faire ici '... End Sub End Class
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question
3 oct. 2013 à 14:38