03-10-12, 09:25 AM
كود :
Public Class UsbDriveDetect
Private WithEvents m_MediaConnectWatcher As ManagementEventWatcher
Public Enum EnUsbArrivedRemoved
Arrived
Removed
End Enum
Public Event UsbDeviceArrivedRemoved(ByVal sender As Object, ByVal e As UsbDriveEeventArgs)
Public Structure DriveInfoStr
Dim DriveLetter As String
Dim Description As String
Dim FileSystem As String
Dim Size As UInt64
Dim FreeSpace As UInt64
Dim DriveType As DriveTypeEnum
Dim VolumeName As String
Dim VolumeSerialNumber As String
End Structure
Public Enum DriveTypeEnum
Unknown = 0
NoRootDirectory = 1
RemovableDisk = 2
LocalDisk = 3
NetworkDrive = 4
CompactDisc = 5
RAMDisk = 6
End Enum
Sub New()
Dim Query2 As New WqlEventQuery("SELECT * FROM __InstanceOperationEvent WITHIN 1 " _
& "WHERE TargetInstance ISA 'Win32_DiskDrive'")
Me.m_MediaConnectWatcher = New ManagementEventWatcher
Me.m_MediaConnectWatcher.Query = Query2
End Sub
Public Sub StartDetection()
Me.m_MediaConnectWatcher.Start()
End Sub
Public Sub StopDetection()
Me.m_MediaConnectWatcher.Stop()
End Sub
Public Class UsbDriveEeventArgs
Inherits System.EventArgs
Private m_DeviceName As String
Private m_DriveLetter As String
Private m_ArrivedRemoved As EnUsbArrivedRemoved
Private m_DriveInfoS As DriveInfoStr
Sub New(ByVal DeviceName As String, ByVal DriveLetter As String, ByVal ArrivedRemoved As EnUsbArrivedRemoved _
, ByVal DriveInformations As DriveInfoStr)
Me.m_DeviceName = DeviceName
Me.m_DriveLetter = DriveLetter
Me.m_ArrivedRemoved = ArrivedRemoved
Me.m_DriveInfoS = DriveInformations
End Sub
Public ReadOnly Property DriveInformation() As DriveInfoStr
Get
Return m_DriveInfoS
End Get
End Property
Public ReadOnly Property DeviceName() As String
Get
Return Me.m_DeviceName
End Get
End Property
Public ReadOnly Property DriveLetter() As String
Get
Return Me.m_DriveLetter
End Get
End Property
Public ReadOnly Property ArrivedRemoved() As EnUsbArrivedRemoved
Get
Return Me.m_ArrivedRemoved
End Get
End Property
End Class
Private Sub m_MediaConnectWatcher_EventArrived(ByVal sender As Object, ByVal e As System.Management.EventArrivedEventArgs) Handles m_MediaConnectWatcher.EventArrived
Dim mbo, obj As ManagementBaseObject
' the first thing we have to do is figure out if this is a creation or deletion event
mbo = CType(e.NewEvent, ManagementBaseObject)
' next we need a copy of the instance that was either created or deleted
obj = CType(mbo("TargetInstance"), ManagementBaseObject)
If obj("InterfaceType") = "USB" Then
Select Case mbo.ClassPath.ClassName
Case "__InstanceCreationEvent"
Dim DrL As String = GetDriveLetterFromDisk(obj("Name"))
Dim Ee As New UsbDriveEeventArgs(obj("Caption"), DrL, EnUsbArrivedRemoved.Arrived, GetDriveInformation(DrL))
RaiseEvent UsbDeviceArrivedRemoved(Me, Ee)
Case "__InstanceDeletionEvent"
Dim Ee As New UsbDriveEeventArgs(obj("Caption"), Nothing, EnUsbArrivedRemoved.Removed, Nothing)
RaiseEvent UsbDeviceArrivedRemoved(Me, Ee)
End Select
End If
End Sub
Private Function GetDriveLetterFromDisk(ByVal Name As String) As String
Dim oq_part, oq_disk As ObjectQuery
Dim mos_part, mos_disk As ManagementObjectSearcher
Dim obj_part, obj_disk As ManagementObject
Dim ans As String = ""
' WMI queries use the "\" as an escape charcter
Name = Replace(Name, "\", "\\")
' First we map the Win32_DiskDrive instance with the association called
' Win32_DiskDriveToDiskPartition. Then we map the Win23_DiskPartion
' instance with the assocation called Win32_LogicalDiskToPartition
oq_part = New ObjectQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & Name & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition")
mos_part = New ManagementObjectSearcher(oq_part)
For Each obj_part In mos_part.Get()
oq_disk = New ObjectQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & obj_part("DeviceID") & """} WHERE AssocClass = Win32_LogicalDiskToPartition")
mos_disk = New ManagementObjectSearcher(oq_disk)
For Each obj_disk In mos_disk.Get()
ans &= obj_disk("Name") & ","
Next
Next
Return ans.Trim(","c)
End Function
Private Function GetDriveInformation(ByVal DriveLetter As String) As DriveInfoStr
Dim Query As String = "Select * from Win32_LogicalDisk WHERE DeviceID = '" & DriveLetter & "'"
Dim colDisks As New ManagementObjectSearcher(Query)
For Each objDisk As ManagementObject In colDisks.Get
Dim DrIn As DriveInfoStr
With DrIn
.DriveLetter = objDisk("DeviceID")
.FileSystem = objDisk("FileSystem")
.Size = objDisk("Size")
.FreeSpace = objDisk("FreeSpace")
.Description = objDisk("Description")
.DriveType = objDisk("DriveType")
.VolumeName = objDisk("VolumeName")
.VolumeSerialNumber = objDisk("VolumeSerialNumber")
End With
Return DrIn
Next
Return Nothing
End Function
End Classوفيمايلي مثال عن الاستخدام
عرف متغيرا عاما يشير إلى فئتنا ضمن فئة النموذج
كود :
Private WithEvents UsbMonitor As New UsbDriveDetectكود :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.UsbMonitor.StartDetection()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.UsbMonitor.StopDetection()
End Subكود :
Private Sub UsbMonitor_UsbDeviceArrivedRemoved(ByVal sender As Object, ByVal e As UsbDriveDetect.UsbDriveEeventArgs) _
Handles UsbMonitor.UsbDeviceArrivedRemoved
MsgBox(e.DeviceName & ControlChars.CrLf & e.DriveLetter & ControlChars.CrLf & e.ArrivedRemoved.ToString)
End Sub