Link to home
Start Free TrialLog in
Avatar of adam_pedley
adam_pedley

asked on

Detect USB Pen Drives

Hi

Is it possible in vb to detect any USB Pen drives attached to the computer?

Thanks
Adam
Avatar of flavo
flavo
Flag of Australia image

Avatar of adam_pedley
adam_pedley

ASKER

Thanks

but how do i scan already existing drives to find a flash drive, i cant always depend that my program will be loaded before the flash drive is plugged in
ASKER CERTIFIED SOLUTION
Avatar of armoghan
armoghan
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is how you can get all the drives
http://www.csharphelp.com/archives2/archive363.html
Thanks

Here is the final code I came up with that detects all drives and finds out what type they are


    Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Integer

    Private Sub Detect_Drives()
        txtDrives.Clear()
        Try
            Dim str() As String = Directory.GetLogicalDrives()
            Dim i As Integer = 0
            While i < str.Length
                txtDrives.AppendText(str(i) & "     " & GetDriveType(str(i)) & vbCrLf)
                i += 1
            End While
        Catch ex As IOException
            MsgBox(ex.Message)
        End Try
End Sub
Also remember to import

imports system.io

One error i did find is that the site http://custom.programming-in.net/articles/art9-1.asp?f=GetDriveType thinks flash drives are type # 6 but all my flash drives come back as type # 2