Private Const WM_DEVICECHANGE As Integer = &H219
Private Const DBT_DEVICEARRIVAL As Integer = &H8000
Private Const DBT_DEVICE
Private Const DBT_DEVTYP_VOLUME As Integer = &H2
Private Structure DEV_BROADCAST_VOLUME
Dim Dbcv_Size As Integer
Dim Dbcv_Devicetype As Integer
Dim Dbcv_Reserved As Integer
Dim Dbcv_Unitmask As Integer
Dim Dbcv_Flags As Short
End Structure
Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)
On Error Resume Next
If M.Msg = WM_DEVICECHANGE Then
Select Case M.WParam
Case DBT_DEVICEARRIVAL
Dim DevType As Integer = Marshal.ReadInt32(M.LParam, 4)
If DevType = DBT_DEVTYP_VOLUME Then
Dim Vol As New DEV_BROADCAST_VOLUME
Vol = Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
If Vol.Dbcv_Flags = 0 Then
For i = 0 To 20
If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
Dim Usb As String = Chr(65 + i) + ":\"
Msgbox (Usb)
Exit For
End If
Next
End If
End If
End Select
End If
MyBase.WndProc(M)
End Sub