Link to home
Start Free TrialLog in
Avatar of MiDaTi
MiDaTi

asked on

Is a drive a USB drive?

I need to check to see if a drive is connected via USB. According to the documentation for GetDriveType you need to call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property. I'm not having any luck getting this to work. Can someone post a function that accepts a drive letter and returns TRUE if the drive is connected via USB.
Avatar of MiDaTi
MiDaTi

ASKER

Sorry, but you've misunderstood the question. I don't want to know when a USB device is connected, I want to know if a drive is connected via USB.
Avatar of MiDaTi

ASKER

Just to clarify further: I want to know if an already connected drive is connected via USB, e.g. it is a USB thumb drive. I don't need (or want) to know when it is connected or disconnected. According to the Win32 API documentation you need to use SetupDiGetDeviceRegistryProperty to find this out.
can you use that simple solution
{*******************************************************************************
 *    Read all the drive in a computer system ,
 *    V 1.0   incl. removable drives like Floppy or USB Stick
 *
 *******************************************************************************}
 
function GetDriveList : TStringList;
var  DriveList  :  TStringList;
     I,Typ      :  Integer;
     s          :  String;
begin
 
  try
  DriveList:= TStringList.Create;
  DriveList.Clear;
 
  For i := 0 To 25 do
    begin
      s := Chr(i + 65) + ':\';
      typ := GetDriveType(PChar(s));
      If Typ <> 0 Then
        Case Typ of
          DRIVE_REMOVABLE :
            DriveList.Add(Chr(i + 65) + ': Floppy/USB');
 
          DRIVE_FIXED :
            DriveList.Add(Chr(i + 65) + ': HARD DISC');
 
          DRIVE_CDROM :
            DriveList.Add(Chr(i + 65) + ': CDROM');
 
          DRIVE_RAMDISK :
            DriveList.Add(Chr(i + 65) + ': RAM DISC');
 
          DRIVE_REMOTE :
            DriveList.Add(Chr(i + 65) + ': NET DRIVE');
        end;
    end;
    finally   { free ? }
     Result :=  DriveList;
 
    end;
 
end;

Open in new window

Avatar of MiDaTi

ASKER

No, this will not work. GetDriveType returns DRIVE_FIXED for a USB key (it does for me on XP SP3). As per the Win32 documentation for GetDriveType: you need to call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property to discover if it is connected via USB.
ASKER CERTIFIED SOLUTION
Avatar of MiDaTi
MiDaTi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial