Results 1 to 28 of 28

Thread: [RESOLVED] Detecting Drive Letter of USB Drive

Threaded View

  1. #2

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Detecting Drive Letter of USB Drive

    OK I got it working
    Just in case anyone else ever wants to do a similar thing, here's my code:

    EDIT: See updated code in post #13 for a better version

    vb Code:
    1. Imports System.Runtime.InteropServices
    2. Public Class Form1
    3.  
    4.     Private Const WM_DEVICECHANGE As Integer = &H219
    5.     Private Const DBT_DEVICEARRIVAL As Integer = &H8000
    6.     Private Const DBT_DEVTYP_VOLUME As Integer = &H2
    7.  
    8.     'Device information structure
    9.     Public Structure DEV_BROADCAST_HDR
    10.         Public dbch_size As Int32
    11.         Public dbch_devicetype As Int32
    12.         Public dbch_reserved As Int32
    13.     End Structure
    14.  
    15.     'Volume information Structure
    16.     Private Structure DEV_BROADCAST_VOLUME
    17.         Public dbcv_size As Int32
    18.         Public dbcv_devicetype As Int32
    19.         Public dbcv_reserved As Int32
    20.         Public dbcv_unitmask As Int32
    21.         Public dbcv_flags As Int16
    22.     End Structure
    23.  
    24.     '<<<< Function that gets the drive letter from the unit mask >>>>
    25.     Private Function GetDriveLetterFromMask(ByRef Unit As Int32)
    26.         Dim i As Integer
    27.         For i = 0 To 25
    28.             If Unit And i Then Exit For
    29.             Unit = Unit >> 1
    30.         Next
    31.         Return Chr(i + 1 + Asc("A"))
    32.     End Function
    33.  
    34.     'Override message processing to check for the DEVICECHANGE message
    35.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    36.         If m.Msg = WM_DEVICECHANGE Then
    37.             If m.WParam = DBT_DEVICEARRIVAL Then
    38.                 Dim DeviceInfo As DEV_BROADCAST_HDR
    39.                 DeviceInfo = Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR))
    40.                 If DeviceInfo.dbch_devicetype = DBT_DEVTYP_VOLUME Then
    41.                     Dim Volume As DEV_BROADCAST_VOLUME
    42.                     Volume = Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_VOLUME))
    43.                     Dim DriveLetter As String = (GetDriveLetterFromMask(Volume.dbcv_unitmask) & ":\")
    44.                     Dim FileList() As String = IO.Directory.GetFiles(DriveLetter, "test.txt")
    45.                     If FileList.Count > 0 Then
    46.                         MessageBox.Show("Found Config File")
    47.                         '<<<< The test file has been found >>>>
    48.                         Else
    49.                         MessageBox.Show("Could not find config file")
    50.                         '<<<< Test file has not been found >>>>
    51.                     End If
    52.                 End If
    53.             End If
    54.         End If
    55.         MyBase.WndProc(m)
    56.     End Sub
    57.  
    58.    
    59. End Class
    Last edited by chris128; Nov 9th, 2009 at 05:44 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width