none
How to Detect USB Flash Drive

    Question

  • I am making a program that will allow a user to use any USB flash drive as a key, that when inserted, will automatically (upon validation, of course) enter login and password data into web forms, applications, and maybe even Windows Logon. But first things first: how to I detect when a USB key is inserted into the drive, for instance, perhaps just detect the last/latest inserted drive?

    In addition, does anyone know if there is a way to directly identify a flash drive, maybe based on a serial number?, rather than validating it from a file on the device?

    Anyway, the first question is the most important.

    Thanks for any help.

    Tuesday, February 27, 2007 7:04 PM

All replies

  • You can AutoRun enable your application, just like you would a CD, and this will work as long as the user has the .NET Framework installed and AutoRun turned on (Hint: Many users are disabling this after the Sony CD rootkit installer disaster).

    If you have your software already installed on the target system, you could watch for drive changes and see if it was your USB drive was the one being mounted and then activate your app. Some medical device flash card readers use this method.

    As for ID'ing the device, this is a manufacturer thing. If you're going to use a specific brand and model of USB, then you can get the specs for it and use this to determine if it's yours or not. That's another thing I've seen done on medical device SD cards. But if it can be any USB drive, then a file based method is going to be your best shot.

     

    Tuesday, February 27, 2007 8:16 PM
  • "If you have your software already installed on the target system, you could watch for drive changes and see if it was your USB drive was the one being mounted and then activate your app. Some medical device flash card readers use this method"

    This is precisely what I want to do. When the USB drive is detected, say by its name (excluding the drive letter, for this is auto assigned, and varies), the program installed on the target system (running in the system tray) will search for a file on the USB drive, and if it is present, enter the required username/password, which is stored on another file on the drive.

    What I would much appreciate is a code sample that would show how to do this. As I stated before, I can't simply look for the validation file at a specific file address, because the drive letter can vary. For this reason, I thought that perhaps I could detect as soon as a USB drive is inserted, and look for the file on the most recently inserted drive.

    My problem is that I don't know how I would go about doing this, and I was hoping someone could provide a code sample.

    Thanks for all of your help.

    Tuesday, February 27, 2007 10:16 PM
  • This thread looks like it has some information on how to do this: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=948526&SiteId=1

    Give it a try and see it it will work in your situation.

    Wednesday, February 28, 2007 2:01 PM
  • Unfortunately this doesn't help much, because it's for a console app and I need it for a Windows App for Visual Basic 2005. (Sorry I didn't specify before)

    Probably the most useful example would have a form that displays a message any time the app detects the insertion or removal of a device. I found several examples on the web but they are all coded in Visual Basic 6, and for some reason I can't even open them to upgrade them.

    Thanks for all of your help.

    Friday, March 02, 2007 9:46 PM
  • Anyone have any ideas/ suggestions?
    Tuesday, March 06, 2007 2:40 AM
  •  blabus wrote:

    Unfortunately this doesn't help much, because it's for a console app and I need it for a Windows App for Visual Basic 2005. (Sorry I didn't specify before)

    I just tried that code and it worked fine in a WinForms app for me. Make sure that you import System.Management into the class/form you're using and add a reference to it to your project.

    Tuesday, March 06, 2007 2:03 PM
  • Here's a thread with a couple of examples, mine is insanely complicated  and not recommended.

    http://www.xtremevbtalk.com/showthread.php?t=264918

    Unfortunately though, USB keys don't tend to have serial numbers on them (there's a field for it, but it is usually empty).

    (You could query for them by p/invoking deviceIOControl and sending IOCTL_STORAGE_GET_MEDIA_SERIAL_NUMBER, on >XP, but it would come back blank for most keys.)

    Tuesday, March 06, 2007 4:52 PM
  • I thought that might have been a problem, because everything that had System.Management was underlined red.

    How do I import System.Management and add a reference to it?

    Thanks again for all of the help you've provided.

    Tuesday, March 06, 2007 5:53 PM
  • To add the reference to System.Management, go to My Project|References and click on the Add button. Select System.Management from the list of .NET references and it will be added to your project. In the modules where you'll be using it, just add Imports System.Management at the top of the page.
    Tuesday, March 06, 2007 8:12 PM
  • Thanks, the code does work fine. However, do you know how I could check for the device label, that is, when you right-click on the flash drive in My Computer and click Rename. I tried adding

    And obj("VolumeLabel") = "Device Label"

    after the part that checks to see if the the drive type is USB when a drive is attached, but it does not work.

    Basically, in my security program, I want to check for a specific USB flash drive, and seeing as the drive letter can vary for the same device, and the device name (i.e. "Apple iPod USB Device") can be the same for multiple USB drives, I believe the only way to uniquely identify it would be by the label.

    Is there a way to do this? (Perhaps in the code you originally supplied)

    Thanks again for all of your help on this subject. I would have never gotten this far!

     

    Wednesday, March 07, 2007 1:30 AM
  • Alright, I'm starting to get everything working, but I need a little more help. Basically, all I need to know is the absolute simplest way to execute some code when my program detects when any device is inserted (be it USB flash drive, memory card, ect.)

    Basically, just a trigger that fires when the progam detects the insertion (and removal) of any device on the machine.

    Thanks.

    Wednesday, March 07, 2007 11:21 PM
  • My USB is not working. It has a giant read x over it but it worked two days before.
    • Proposed as answer by madddog mark Thursday, February 17, 2011 1:29 PM
    Friday, April 06, 2007 8:34 PM
  • This will get the info of your drives including usb

    Imports Scripting
    Imports System.IO
    
    
    Public Class USBsn
    
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim driveNames As New List(Of String)
        For Each drive As DriveInfo In My.Computer.FileSystem.Drives
          Try
            Dim fso As Scripting.FileSystemObject
            Dim oDrive As Drive
    
            fso = CreateObject("Scripting.FileSystemObject")
    
            oDrive = fso.GetDrive(drive.Name)
    
            ListBox1.Items.Add(drive.Name & "  " & oDrive.SerialNumber)
          Catch ex As Exception
    
          End Try
    
        Next
    
    
    
       
    
    
      End Sub
    End Class
    
    Thursday, February 17, 2011 1:30 PM
  • My USB is not working. It has a giant read x over it but it worked two days before.
    Fry, firstly is your question related to this thread? Is it about Visual Basic at all? Or it's just that your USB is no longer working? Also, if it's about VB.NET and related to this thread please post some code. If it's not about this at all go find the relevant forum or post a new question.

    Test
    Thursday, February 17, 2011 2:19 PM
  • Option Strict On
    Public Class Form1
      Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim driveNames As New List(Of String)
        For Each drv As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives
          If drv.DriveType = IO.DriveType.Removable AndAlso drv.IsReady Then
            Dim drvLetter As String = drv.Name
            'Dim drvVolume As String = drv.VolumeLabel
            ' Dim drvSpaceAvailable As String = ShowTotalFileSize(drv.AvailableFreeSpace)
            If ListBox1.Items.IndexOf(drv.Name) = -1 Then
              If ListBox2.Items.IndexOf(drv.Name) = -1 Then
                MsgBox(drv.Name & " has been detected.", MsgBoxStyle.Information, "New Drive")
                ListBox2.Items.Add(drv.Name)
              End If
            End If
          End If
        Next
      End Sub
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Interval = 1000
      End Sub
      'Private Function ShowTotalFileSize(ByVal bytes As Long) As String
      ' Dim stringToReturn As String = ""
      '   If bytes < 1024 Then
      '     stringToReturn = Format(bytes, "#,##0.0") & " bytes"
      '   ElseIf bytes < (1024 * 1024) Then
      '     stringToReturn = Format((bytes / 1024), "#,###") & " kB"
      '   ElseIf bytes >= (1024 * 1024) And bytes < (1024 * 1024 * 1024) Then
      '     stringToReturn = Format((bytes / (1024 * 1024)), "#,###.0") & " Megs"
      '   ElseIf bytes >= (1024 * 1024 * 1024) Then
      '     stringToReturn = Format((bytes / (1024 * 1024 * 1024)), "#,###.00") & " Gigs"
      '   End If
      '   Return stringToReturn
      ' End Function
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim driveNames As New List(Of String)
        For Each drv As System.IO.DriveInfo In System.IO.DriveInfo.GetDrives
          If drv.DriveType = IO.DriveType.Removable AndAlso drv.IsReady Then
            Dim drvLetter As String = drv.Name
            'Dim drvVolume As String = drv.VolumeLabel
            ' Dim drvSpaceAvailable As String = ShowTotalFileSize(drv.AvailableFreeSpace)
            ListBox1.Items.Add(drv.Name)
          End If
        Next
      End Sub
    
      Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Timer1.Enabled = True
      End Sub
    End Class
    
    

    First press button1 to get all the current drives into listbox1. Then press button3 to start the timer, which will check if the drives are in listbox1. If they are, it means they are already there. If they arent, it means they just got plugged in, and therefore will have a messagebox with the message.

    Hope this helps

    Samuel


    Test
    Thursday, February 17, 2011 2:33 PM