I need to show the USB connected to my computer and how to disconnect the USB as well as put it, remove or put the letter of USB corresponding in a ComboBox . I want to do it in C # , I have this code but it is in Visual Basic .
Protected Overrides Sub WndProc(ByRef M As System.Windows.Forms.Message)'
'These are the required subclassing codes for detecting device based removal and arrival.
'
If M.Msg = WM_DEVICECHANGE Then
Select M.WParam
'
'Check if a device was added.
Case DBT_DEVICEARRIVAL
Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
If DevType = DBT_DEVTYP_VOLUME Then
Dim Vol As New DEV_BROADCAST_VOLUME
Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
If Vol.Dbcv_Flags = 0 Then
For i As Integer = 0 To 20
If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
Dim usb = From getInfo In System.IO.DriveInfo.GetDrives
Dim d As IO.DriveInfo
For Each d In usb
If d.IsReady = True AndAlso d.DriveType = IO.DriveType.Removable Then
unidades.Items.Add(d.Name & d.VolumeLabel).ToString()
End If
Next
Exit For
End If
Next
End If
End If
... and I have this code to detect the removal of the USB , but it is not removed from the ComboBox , I do not know why.
Case DBT_DEVICEREMOVECOMPLETE
Dim DevType As Integer = Runtime.InteropServices.Marshal.ReadInt32(M.LParam, 4)
If DevType = DBT_DEVTYP_VOLUME Then
Dim Vol As New DEV_BROADCAST_VOLUME
Vol = Runtime.InteropServices.Marshal.PtrToStructure(M.LParam, GetType(DEV_BROADCAST_VOLUME))
If Vol.Dbcv_Flags = 0 Then
For i As Integer = 0 To 20
If Math.Pow(2, i) = Vol.Dbcv_Unitmask Then
Dim usb = From getInfo In System.IO.DriveInfo.GetDrives
Dim d As IO.DriveInfo
For Each d In usb
If d.IsReady = False AndAlso d.DriveType = IO.DriveType.Removable Then
unidades.Items.Remove(d.Name & d.VolumeLabel & usb.ToString)
End If
Next
Exit For
End If
Next
End If
End If
End Select
End If
MyBase.WndProc(M)