c# – Detect USB in C #

Question:

I need to show the USB connected to my computer and that both when disconnecting the USB and putting it in, remove or put the letter of the corresponding USB 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 don't 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)

Answer:

Maybe a little late I already had it implemented like this inside a form:

private void Form1_Load(object sender, EventArgs e)
{
    refreshUsb();
}
const int WM_DEVICECHANGE = 537;
const int DBT_DEVICEARRIVAL = 0x8000;
const int DBT_DEVICEREMOVECOMPLETE = 0x8004;
protected override void WndProc(ref Message m)
{
    if (m.Msg == WM_DEVICECHANGE&&((int)m.WParam==DBT_DEVICEARRIVAL ||(int)m.WParam==DBT_DEVICEREMOVECOMPLETE ))            
        refreshUsb();

    base.WndProc(ref m);
}

private void refreshUsb()
{
    var usbs = System.IO.DriveInfo.GetDrives().Where(d => d.DriveType == System.IO.DriveType.Removable).ToArray();
    comboBox1.Items.Clear();
    comboBox1.Items.AddRange(usbs);
}
Scroll to Top