Detect when Removable USB/Volume Drive Devices are Added/Removed/Connected – VB.NET

  • by

Below is some code I saw on a vb message board along time ago but I can’t remember where. This source code uses mainly pure code without having to rely on a specific class or 3rd Party controls. The downside is that it is pretty complicated. But if I can get the formatting and stuff right you should be able to do a simple copy and paste. I made some minor code changes and tested the code and I have it throw a messagebox with the drive letter of the new usb device being attached. It does use application subclassing to intercept the messages and checks if any of them are a new removable volume or not. If it is then it will parse the drive letter of the newly attached device and throw a messagebox letting you know. Most usb devices such as flash drives (Thumb Drives or Pen Drive’s as they are also called), external hard drives, and such with a removable disk volume of some sort would be detected just fine. In all of my personal testing this code did it all! You can use this code with VB.NET, Visual Basic 2005, Visual Basic.NET 2008, and VB 2010 to check for the arrival of usb volume devices. You can also make some changes to make it work for VB 6.0 as well. IIRC Visual Basic 6.0 will need a addiional API call to do the subclassing portion.

'
'Used to detected if any of the messages are any of these constants values.
Private Const WM_DEVICECHANGE As Integer = &H219

Private Const DBT_DEVICEARRIVAL As Integer = &H8000

Private Const DBT_DEVICEREMOVECOMPLETE As Integer = &H8004

Private Const DBT_DEVTYP_VOLUME As Integer = &H2  '
'
'Get the information about the detected volume.
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

Now for the subclassing based code. This is where it starts to get alittle difficult…

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 Case 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 As String = Chr(65 + i) + ":\"

                                MsgBox("Looks like a USB device was plugged in!" & vbNewLine & vbNewLine & "The drive letter is: " & Usb.ToString)

                                Exit For

                            End If

                        Next

                    End If

                End If
                '
                'Check if the message was for the removal of a device.
            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 As String = Chr(65 + i) + ":\"

                                MsgBox("Looks like a volume device was removed!" & vbNewLine & vbNewLine & "The drive letter is: " & Usb.ToString)

                                Exit For

                            End If

                        Next

                    End If

                End If

        End Select

    End If

    MyBase.WndProc(M)

End Sub

Unfortunately the code is kinda complicated but it Does work. You may have to tinker with the code alittle since wordpress does a pretty bad job of formatting even though I insert the source code exactly the way it should be. I may add a example application eventually available for download. Hopefully this may help someone. Have Fun!          

Jason

Leave a Reply

Your email address will not be published. Required fields are marked *