[RESOLVED] Detecting Drive Letter of USB Drive-VBForums
Results 1 to 28 of 28

Thread: [RESOLVED] Detecting Drive Letter of USB Drive

  1. #1

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

    Resolved [RESOLVED] Detecting Drive Letter of USB Drive

    OK so this is kind of following on from someone else's thread that I am trying to help out with (http://www.vbforums.com/showthread.php?t=534932).
    If you cant be bothered to read the other thread, here's a brief summary of what I (and the OP in that thread) would like to achieve:

    When a USB drive is plugged in, the application automatically searches this drive for a specific file.

    So to cut a long story short, this is what I have so far:
    I am using VB.NET by the way, not VB6
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Const WM_DEVICECHANGE As Integer = &H219
    4.     Private Const DBT_DEVICEARRIVAL As Integer = 32768
    5.  
    6.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    7.         If m.Msg = WM_DEVICECHANGE Then
    8.             If m.WParam = DBT_DEVICEARRIVAL Then
    9.                 MessageBox.Show("Removable Drive Detected")
    10.             End If
    11.         End If
    12.         MyBase.WndProc(m)
    13.     End Sub
    14. End Class
    This works in that when a USB drive is plugged in, it pops up with the messagebox. It took me a lot of trial and error and a lot of reading on the MSDN site to get even this much working (this is only the 3rd or 4th API I have ever tried to use) as there didnt seem to be much useful stuff on google... but anyway yeah, the problem is that its all well and good detecting that a drive was connected but now we need to be able to scan it. So we need to get the drive letter that was assigned to the newly connected drive - According to the MSDN site, the following is passed in through the lparam property of the DEVICECHANGE message:
    A pointer to a structure identifying the device inserted. The structure consists of an event-independent header, followed by event-dependent members that describe the device. To use this structure, treat the structure as a DEV_BROADCAST_HDR structure, then check its dbch_devicetype member to determine the device type.
    Looking at the DEV_BROADCAST_HDR structure and its dbch_devicetype member in particular I can see that I can check to see if the device is a Volume. Then if it is a volume I can check the DEV_BROADCAST_VOLUME structure member named dbcv_unitmask and this relates to the drive letter that is assigned. Hopefully I didnt explain that too badly... The problem is, like I said, I'm fairly new to APIs and so I havent got a clue how to do what I just explained!
    I'm guessing I need to just declare each of these structures and then it will be fairly easy to use them in my managed code but none of the mentioned structures appear in my API Viewer program so I have no idea what type of object each member needs to be declared as etc

    Anyone help me out?

    Thanks
    Chris
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  2. #2

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

    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


  3. #3
    New Member
    Join Date
    Aug 2007
    Posts
    9

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Hi. thanks for your code.

    It gives me the wrong drive letter. if I use 1 usb pen, windows says its F: and the code says its G:. can you help me?

  4. #4

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Try replacing this part of the code:
    vb Code:
    1. '<<<< Function that gets the drive letter from the unit mask >>>>    
    2. Private Function GetDriveLetterFromMask(ByRef Unit As Int32)
    3. Dim i As Integer      
    4. For i = 0 To 25
    5.  If Unit And i Then Exit For
    6.    Unit = Unit >> 1
    7.  Next
    8. Return Chr(i + 1 + Asc("A"))
    9. End Function
    with this:
    vb Code:
    1. '<<<< Function that gets the drive letter from the unit mask >>>>    
    2. Private Function GetDriveLetterFromMask(ByRef Unit As Int32)
    3. Dim i As Integer      
    4. For i = 0 To 25
    5.  If Unit And i Then Exit For
    6.    Unit = Unit >> 1
    7.  Next
    8. Return Chr(i + Asc("A"))
    9. End Function
    Any better?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  5. #5
    New Member
    Join Date
    Aug 2007
    Posts
    9

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    I already did that but still doesn't work.

    If I change to:

    Code:
    If Unit And i Then msgbox(Chr(i + 1 + Asc("A")))
    It prints G: , H: and I: and the real letter is H:. With another usb pen prints F: and the real letter is G:

  6. #6

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Make your mind up, a moment ago you said Windows says its F and the code says its G, now you say its the other way around..
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  7. #7

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    I just tested it out on a load of PCs and with the original code I provided it does seem to be one letter out all of the time. But if you just change this line:

    vb Code:
    1. Return Chr(i + 1 + Asc("A"))

    To this:

    vb Code:
    1. Return Chr(i + Asc("A"))

    Then it works fine on all the machines I tested on.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  8. #8
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    I found another thread to bookmark in case I need this code later on. Thanks

  9. #9
    PowerPoster kevininstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    5,868

    Re: Detecting Drive Letter of USB Drive

    Quote Originally Posted by chris128 View Post
    OK I got it working
    Just in case anyone else ever wants to do a similar thing, here's my code:
    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
    So how does one get this code to work with
    Option explicit = On
    Option strict = On

    There are many problems with them set to On that will not allow this code to compile.

  10. #10
    Hyperactive Member _powerade_'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    354

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Hi Chris,

    I was trying your code, but it seems to have a few issues:

    EDIT: Tested with Vista (if it makes any difference...)

    1st

    Tested with VB 2008 Express

    vb.net Code:
    1. 'Override message processing to check for the DEVICECHANGE message
    2.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    3.         If m.Msg = WM_DEVICECHANGE Then
    4.             If m.WParam = DBT_DEVICEARRIVAL Then
    5.                 Dim DeviceInfo As DEV_BROADCAST_HDR
    6.  
    7.                 DeviceInfo = Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR))
    8.  
    9.                 If DeviceInfo.dbch_devicetype = DBT_DEVTYP_VOLUME Then
    10.                     Dim Volume As DEV_BROADCAST_VOLUME
    11.                     Volume = Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_VOLUME))
    12.                     Dim DriveLetter As String = (GetDriveLetterFromMask(Volume.dbcv_unitmask) & ":\")
    13.  
    14.                     MessageBox.Show(DriveLetter) '<-- Shows G:\, but should be I:\...  
    15.  
    16.                     Dim FileList() As String = IO.Directory.GetFiles(DriveLetter, "test.txt")
    17.  
    18.                     If FileList.Count > 0 Then
    19.                         MessageBox.Show("Found Config File")
    20.                         '<<<< The test file has been found >>>>
    21.                     Else
    22.                         MessageBox.Show("Could not find config file")
    23.                         '<<<< Test file has not been found >>>>
    24.                     End If
    25.  
    26.                 End If
    27.             End If
    28.         End If
    29.  
    30.         MyBase.WndProc(m)
    31.     End Sub

    Messagebox says the drive letter is G:\, but the actual drive is I:\, so the following codeline throws a System.IO.DirectoryNotFoundException.

    vb.net Code:
    1. Dim FileList() As String = IO.Directory.GetFiles(DriveLetter, "test.txt")

    So when I change the DriveLetter string in the following code to "I:\" the code works fine.

    vb.net Code:
    1. Dim FileList() As String = IO.Directory.GetFiles("I:\", "test.txt")

    2nd
    When testing your code in VB2010 (b2), the WndProc is making trouble (see images).
    Attached Images Attached Images   
    Last edited by _powerade_; Nov 9th, 2009 at 02:13 PM. Reason: Typos
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem

  11. #11

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Have you made the changes mentioned in post #7?

    As for VS 2010 I havent got that installed so cant test it but I dont think it should be any different... You do know that you have to put that WndProc part in a Form right? You cant just stick that in some random class as your average class wont have a WndProc method for you to override

    I just tested it on my Windows 7 PC and it worked fine - although strangely it was the original code that worked (ie without the modifications mentioned in post #7). Cant understand why it seems to be different on different machines :S
    Last edited by chris128; Nov 9th, 2009 at 04:36 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  12. #12

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

    Re: Detecting Drive Letter of USB Drive

    Quote Originally Posted by kevininstructor View Post
    So how does one get this code to work with
    Option explicit = On
    Option strict = On

    There are many problems with them set to On that will not allow this code to compile.
    Just do what Option Strict tells you to do to correct the errors I just turned it on and fixed them (and tidied up a couple of little things in the code) - see code below:

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

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


  13. #13

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    OK I've changed the code a bit and it works every time on my PC and another PC I just tested on - it gets the correct letter all of the time. So can you try out this code below and see if it works for you?

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.  
    5.     Private Const WM_DEVICECHANGE As Integer = &H219
    6.     Private Const DBT_DEVICEARRIVAL As Integer = &H8000
    7.     Private Const DBT_DEVTYP_VOLUME As Integer = &H2
    8.  
    9.     'Device information structure
    10.     Public Structure DEV_BROADCAST_HDR
    11.         Public dbch_size As Int32
    12.         Public dbch_devicetype As Int32
    13.         Public dbch_reserved As Int32
    14.     End Structure
    15.  
    16.     'Volume information Structure
    17.     Private Structure DEV_BROADCAST_VOLUME
    18.         Public dbcv_size As Int32
    19.         Public dbcv_devicetype As Int32
    20.         Public dbcv_reserved As Int32
    21.         Public dbcv_unitmask As Int32
    22.         Public dbcv_flags As Int16
    23.     End Structure
    24.  
    25.     'Function that gets the drive letter from the unit mask
    26.     Private Function GetDriveLetterFromMask(ByRef Unit As Int32) As Char
    27.         For i As Integer = 0 To 25
    28.             If Unit = (2 ^ i) Then
    29.                 Return Chr(Asc("A") + i)
    30.             End If
    31.         Next
    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 CInt(m.WParam) = DBT_DEVICEARRIVAL Then
    38.                 Dim DeviceInfo As DEV_BROADCAST_HDR
    39.                 DeviceInfo = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_HDR)), DEV_BROADCAST_HDR)
    40.                 If DeviceInfo.dbch_devicetype = DBT_DEVTYP_VOLUME Then
    41.                     Dim Volume As DEV_BROADCAST_VOLUME
    42.                     Volume = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(DEV_BROADCAST_VOLUME)), DEV_BROADCAST_VOLUME)
    43.                     Dim DriveLetter As String = (GetDriveLetterFromMask(Volume.dbcv_unitmask) & ":\")
    44.                     If IO.File.Exists(IO.Path.Combine(DriveLetter, "test.txt")) Then
    45.                         '<<<< The test file has been found >>>>
    46.                         MessageBox.Show("Found test file")
    47.                     Else
    48.                         '<<<< Test file has not been found >>>>
    49.                         MessageBox.Show("Could not find test file")
    50.                     End If
    51.                 End If
    52.             End If
    53.         End If
    54.         MyBase.WndProc(m)
    55.     End Sub
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  14. #14
    Hyperactive Member _powerade_'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    354

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Yup, did that

    Hmm... Weird..! When I tried it again now, the code found the usb pen on drive H:, earlier tonight it was G:, but the actual drive letter is still I:...

    Maybe it has something to with logical disks, primary disks, partitions etc... What if a drive with ie 3 partitions only counts as 1 partition in your code??

    Quote Originally Posted by chris128 View Post
    As for VS 2010 I havent got that installed so cant test it but I dont think it should be any different... You do know that you have to put that WndProc part in a Form right? You cant just stick that in some random class as your average class wont have a WndProc method for you to override


    Yes, it is in a Form
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem

  15. #15
    Hyperactive Member _powerade_'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    354

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Oops, I didn't see your last post.. Just give me a few seconds
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem

  16. #16
    Hyperactive Member _powerade_'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    354

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Looks like you might have fixed the problem, it came up with the correct drive letter when I tested it (multiple times ).
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem

  17. #17

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Cool so its all working now? Did you find out what was causing the problem with the WndProc override warning/error?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  18. #18
    Hyperactive Member _powerade_'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    354

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Looks like I did something wrong when I pasted your code into VS2010 yesterday, cause today it works like a charm
    Last edited by _powerade_; Nov 10th, 2009 at 07:40 AM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem

  19. #19

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    lol glad to hear its working
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  20. #20
    New Member
    Join Date
    Dec 2009
    Posts
    2

    Thumbs up Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Hi,
    Just implemented the code for my new application. This works like charm. Thanks to all of you. I just found one problem that is already mentioned in the following code:

    Code:
    Dim i As Integer       
    For i = 0 To 25
     If Unit And i Then Exit For
       Unit = Unit >> 1
     Next 
    Return Chr(i + 1 + Asc("A"))
    The above is failing for me if the connected disk is at G:. The reason is when "Unit" reaches 4, "i" was also 4 and hence it is showing some other drive letter instead of G:.
    I think the problem is same as specified in

    I already did that but still doesn't work.

    If I change to:

    Code:
    If Unit And i Then msgbox(Chr(i + 1 + Asc("A")))
    It prints G: , H: and I: and the real letter is H:. With another usb pen prints F: and the real letter is G:


    I changed the code as follows and it worked perfectly for me now.


    Code:
    Dim driveMappedPath As String = String.Empty
    Dim i As Integer = 0
    
    While unit > 1
    	i += 1
    	unit = unit >> 1
    End While
    
    driveMappedPath = Chr(i + Asc("A"C))
    Return driveMappedPath.Replace(vbNullChar, ":")
    Once again thanks a lot for this valuable information.

  21. #21

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    I think I already addresses that problem in post #13 but glad to see you got it working anyway
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  22. #22
    Junior Member
    Join Date
    Jun 2010
    Posts
    22

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Can any one tell me how to actually use this code?? I am in need of detecting a usb drive and getting the drive letter so this code seems perfect, but I have no earthly idea how to actually implement it. I am looking the code over and I don't see how any of this code is actually getting called and executed.

    Any help?

  23. #23

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    The sub named WndProc that you see used in the code gets automatically called by Windows every time a "Message" is sent to your window (or to all windows).

    vb Code:
    1. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    So to use the code just copy everything below "Public Class Form1" from the code I posted in post #13 and paste that into your form
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

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


  24. #24
    Junior Member
    Join Date
    Jun 2010
    Posts
    22

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Quote Originally Posted by chris128 View Post
    The sub named WndProc that you see used in the code gets automatically called by Windows every time a "Message" is sent to your window (or to all windows).

    vb Code:
    1. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

    So to use the code just copy everything below "Public Class Form1" from the code I posted in post #13 and paste that into your form
    You sir are the MAN!! Many humble thanks for that tidbit!

  25. #25
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    i am having same error as in the post #10. how to rectify

  26. #26
    New Member
    Join Date
    Dec 2011
    Location
    Washington
    Posts
    1

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    Interesting this code works.

  27. #27
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: [RESOLVED] Detecting Drive Letter of USB Drive


  28. #28

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

    Re: [RESOLVED] Detecting Drive Letter of USB Drive

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.