• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 490
  • Last Modified:

How to get the USB drie letter

Hello all

I want to write a utility whcih finds the drive letters of the USB flash drive inserted into the system.Kindly help me out in this problem.
0
talhaamin
Asked:
talhaamin
  • 4
  • 3
  • 2
  • +2
1 Solution
 
pambooCommented:
The below ex program is written in VB 6.0 uisng win32 APIs. Hope this helps u .

Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long

Private Sub Form_Load()
   
    Dim strSave As String
    Dim driveLetter As String
   
    'Set the graphic mode to persistent
    Me.AutoRedraw = True
   
    'Create a buffer to store all the drives
    strSave = String(255, Chr$(0))
   
    'Get all the drives
    ret& = GetLogicalDriveStrings(255, strSave)
   
    'Extract the drives from the buffer and check if it removable
    For keer = 1 To 100
        If Left$(strSave, InStr(1, strSave, Chr$(0))) = Chr$(0) Then Exit For
        driveLetter = Left$(strSave, InStr(1, strSave, Chr$(0)) - 1)
       
        'Get information about the drive
       Select Case GetDriveType(driveLetter)
         
         Case 2
           
            ' USB flash drive is a removable type.
            Me.Print driveLetter & " Type: Removable"
           
            'But floppy drives also comes under this category .
            'Since they r not dynamic drives like USB , the USB drive
            'letter can be predicted by neglecting the know floppy drive letters.

'         Case 3
'            Me.Print "Drive Fixed" 'Hard disk
'         Case Is = 4
'            Me.Print "Remote" 'network map drive
'         Case Is = 5
'            Me.Print "Cd-Rom"
'         Case Is = 6
'            Me.Print "Ram disk"
'         Case Else
'            Me.Print "Unrecognized"

       End Select

       strSave = Right$(strSave, Len(strSave) - InStr(1, strSave, Chr$(0)))
       
    Next keer

   
    'Thanks to KPD-Team 1998 - Code extracted from allapi.net
   
End Sub


- vm
0
 
adameeCommented:
Essentialy what is above but in C

#include <stdio.h>

int main(){

int i;
char c[4] = {'a',':','\\','\0'};
for(i = 65;i < 91; i++){ // run through all possible drive letters
      c[0] = i;
      switch (GetDriveType(c)){
            
      case 0:
              {
                  //printf("DRIVE_UNKNOWN\n");
                  break;
              }
      case 1:
              {
                  //printf("DRIVE_NO_ROOT_DIR\n");
                  break;
              }
      case 2:
              {
                                               //printf("DRIVE_REMOVABLE\n");
                                                /* USB FLASH DISK WILL BE HERE */
                  break;
              }
      case 3:
              {
                  //printf("DRIVE_FIXED\n");
                  break;
              }
      case 4:
              {
                  //printf("DRIVE_REMOTE\n");
                  break;
              }
      case 5:
              {
                  //printf("DRIVE_CDROM\n");
                  break;
              }
      case 6:
              {
                  //printf("DRIVE_RAMDISK\n");
                  break;
              }
      }
      }

}
0
 
adameeCommented:
might want to return something from main as well not to raise an error :)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
talhaaminAuthor Commented:
But guys when i use this code, my usb type return is fixed drive .Iam confused why
0
 
pambooCommented:
Just to check . R u properly eding the case statement . I mean taking care of break properly .

JAlso just check this below and reply us

1.After instering the USB flash drive , get the drive name using the windows explorer and pass that drive name/letter to
GetDriveType API and check if what type it returns .

eg: GetDriveType("G:\")

2. Also check in the explorer itself , (it should say removable disk on drive G:\ etc.,) . Post us what it shows in the explorer.

Finally , what OS u r using ?

- vm
0
 
pambooCommented:
correction in my prev post
>>R u properly eding the case statement
R u properly ending the case statement ?

0
 
talhaaminAuthor Commented:
In windows explorer My drive type is 'disk drives' and this drive type is also of my C:\ too.Iam using Windows XP and win2k but in both OS iam getting the same drive type Fixed.Iam ending the case statements correctly.
0
 
talhaaminAuthor Commented:
I have a TwinMos usb drive of 16MB capacity
0
 
hal3000Commented:

When using the getDriveType function "removable" refers to the fact that you can remove the media from the drive and replace. It does not mean the drive is removable.

A "removable" drive would be something lile the ZIP drive.

The floppy drive has it's own category, as does cd-roms.

It is possible to get the drive capacity from the drive letter and considering the orders of magnitude difference between your usb drive and your hard drives it might be an easy way to detect the difference. Look into the function "GetDiskFreeSpaceEx". This returns a structure where one of the fields is the number of bytes on the drive.
0
 
talhaaminAuthor Commented:
Dear hal3000 thanks for your reply but the above code is working properly in win2k but not working in winxp.
I want a solution in winxp.Can any body help me.
0
 
hal3000Commented:
I'm confused.

At 9:48 you said that both Win2k and winxp were returning the same result "fixed drive"

That is what the MS documentation says it should return since you cannot change the media in the flash drive (even if it is removable from the system).

I therefore offered an alternative solution.

But your 2:51 message says you are getting different results in XP and 2k.

Does win2k now indicate that the drive is "removable" instead of fixed?
0
 
WilliamSolbergCommented:
I would suggest just putting a file on the flash drive called flashdrive.txt and look for the file..  if it finds it then you got the right drive..
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now