Question

I am trying to determine if a file is on a local drive. I found GetDriveType() WINAPI which retrieves the drive type. However reading the description of the return values it seems, and thats how I understand it, that it retrieves a flash drive as FIXED which is not what I want.

Its working fine on local drives:

bool IsDriveRemovableOrRemote(CString driveRoot)
{
    UINT driveType = GetDriveType(driveRoot);
    return (DRIVE_REMOVABLE == driveType || DRIVE_CDROM == driveType || DRIVE_NO_ROOT_DIR == driveType || DRIVE_REMOTE == driveType);
}

I don't have a flash/external drive to test ATM but I would like if someone can tell me if my interpretation is correct? and if so, what better alternative should I use?

Please bear in mind that I only have the path of the file.

Was it helpful?

Solution

You should read the doco more closely. While a Flash drive is considered a fixed device, there's a note in that linked page:

To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property.

The process seems a little messy if all you start with is the path but you can start reading the doco here. It looks like you may need to enumerate the devices until you find one matching your drive.

To avoid doing this to all your requests, I would do a two-stage check. If your current method says it's not fixed, treat it as non-local.

If it says it is fixed, then you can enumerate the devices using my suggested method to be certain.

Alternatively, you can enumerate all fixed non-USB drives the first time you need to, and then just cache the information. I'm pretty certain that the list of these drives won't change while the system is running - drives that get added and deleted are, by definition, removable.

OTHER TIPS

You can try using DeviceIoControl and query for the BusType = BusTypeUsb by passing IOCTL_STORAGE_QUERY_PROPERTY as its second parameter. Read Determining USB by Thomas Lee at the bottom of page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top