(11-03-20, 12:18 AM)Ali09765 كتب : كود او مثال لعمل نسخه احتياطيه من جميع ملفات usb الى حافضه اخرى في الكمبيوتر
هذا الكود
لم اجربه لكن يمكن يفيدك
كود :
Dim MyDoc As New IO.FileInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\Backup Of Files\test\")
For Each foundFile As String In My.Computer.FileSystem.GetFiles(MyDoc.Directory.FullName, FileIO.SearchOption.SearchAllSubDirectories, "*.txt")
Try
For Each drive As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives
If drive.DriveType = IO.DriveType.Removable AndAlso drive.IsReady Then ' AndAlso drive.AvailableFreeSpace >= 2 * Gig Then
Dim DriveLetter As String = drive.Name
Dim PathToUSBDrive = DriveLetter & IO.Path.GetFileName(foundFile)
My.Computer.FileSystem.CopyFile(foundFile, PathToUSBDrive, True)
End If
Next
Catch ex As Exception
End Try
Nextكود :
Imports System.Runtime.InteropServices
Public Class Form1
Dim Reponse As VbMsgBoxResult
Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIVAL As Integer = &H8000
Private Const DBT_DEVTYP_VOLUME As Integer = &H2
Public Structure DEV_BROADCAST_HDR
Public dbch_size As Int32
Public dbch_devicetype As Int32
Public dbch_reserved As Int32
End Structure
Private Structure DEV_BROADCAST_VOLUME
Public dbcv_size As Int32
Public dbcv_devicetype As Int32
Public dbcv_reserved As Int32
Public dbcv_unitmask As Int32
Public dbcv_flags As Int16
End Structure
Private Function GetDriveLetterFromMask(ByRef Unit As Int32) As Char
For i As Integer = 0 To 25
If Unit = (2 ^ i) Then
Return Chr(Asc("A") + i)
End If
Next
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_DEVICECHANGE Then
If CInt(m.WParam) = DBT_DEVICEARRIVAL Then
Dim DeviceInfo As DEV_BROADCAST_HDR
DeviceInfo = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR)), DEV_BROADCAST_HDR)
If DeviceInfo.dbch_devicetype = DBT_DEVTYP_VOLUME Then
Dim Volume As DEV_BROADCAST_VOLUME
Volume = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_VOLUME)), DEV_BROADCAST_VOLUME)
Dim DriveLetter As String = (GetDriveLetterFromMask(Volume.dbcv_unitmask) & ":\")
If IO.File.Exists(IO.Path.Combine(DriveLetter, "Sauvegarde.txt")) Then
Reponse = MsgBox("Voulez vous sauvegarder votre clé USB?", vbYesNo, "Sauvegarde USB?")
Else
End If
End If
End If
End If
MyBase.WndProc(m)
If Reponse = vbYes Then
Call MsgBox("OUI appuye")
Else
Call MsgBox("NON appuye")
End If
End Sub
End Class
