Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to get the USB drie letter

Posted on 2004-04-08
12
Medium Priority
?
489 Views
Last Modified: 2007-12-19
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
Comment
Question by:talhaamin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
12 Comments
 
LVL 3

Expert Comment

by:pamboo
ID: 10790093
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
 

Expert Comment

by:adamee
ID: 10790148
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
 

Expert Comment

by:adamee
ID: 10790156
might want to return something from main as well not to raise an error :)
0
Independent Software Vendors: 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!

 
LVL 2

Author Comment

by:talhaamin
ID: 10790266
But guys when i use this code, my usb type return is fixed drive .Iam confused why
0
 
LVL 3

Expert Comment

by:pamboo
ID: 10795651
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
 
LVL 3

Expert Comment

by:pamboo
ID: 10795658
correction in my prev post
>>R u properly eding the case statement
R u properly ending the case statement ?

0
 
LVL 2

Author Comment

by:talhaamin
ID: 10795671
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
 
LVL 2

Author Comment

by:talhaamin
ID: 10795672
I have a TwinMos usb drive of 16MB capacity
0
 
LVL 1

Expert Comment

by:hal3000
ID: 10795735

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
 
LVL 2

Author Comment

by:talhaamin
ID: 10796290
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
 
LVL 1

Accepted Solution

by:
hal3000 earned 250 total points
ID: 10796328
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
 

Expert Comment

by:WilliamSolberg
ID: 13759025
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Introduction to Processes

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question