GetDriveType function
Applies to: desktop apps only
Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property.
Syntax
UINT WINAPI GetDriveType( __in_opt LPCTSTR lpRootPathName );
Parameters
- lpRootPathName [in, optional]
-
The root directory for the drive.
A trailing backslash is required. If this parameter is NULL, the function uses the root of the current directory.
Return value
The return value specifies the type of drive, which can be one of the following values.
| Return code/value | Description |
|---|---|
|
The drive type cannot be determined. |
|
The root path is invalid; for example, there is no volume mounted at the specified path. |
|
The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader. |
|
The drive has fixed media; for example, a hard disk drive or flash drive. |
|
The drive is a remote (network) drive. |
|
The drive is a CD-ROM drive. |
|
The drive is a RAM disk. |
Requirements
|
Minimum supported client | Windows XP |
|---|---|
|
Minimum supported server | Windows Server 2003 |
|
Header |
|
|
Library |
|
|
DLL |
|
|
Unicode and ANSI names | GetDriveTypeW (Unicode) and GetDriveTypeA (ANSI) |
See also
Send comments about this topic to Microsoft
Build date: 4/17/2012
- 11/16/2011
- red_zombie
- 8/25/2010
- om92
For example, if C:\moo\cow is a softlink pointing to R:\test, and R a removable drive, GetDrivePath(L"C:\\moo\\cow\\") will return DRIVE_REMOVABLE even though GetDrivePath(L"C:\\") would (typically) return DRIVE_FIXED.
(It would be good to have this guaranteed as part of the API contract/documentation in case it only works by accident right now. It's very useful, e.g. for making policy decisions about where to create temp files based on the type of drive a folder is on while working with links.)
- 7/20/2010
- LeoDavidson
MSDN says
The root directory for the drive. (This would be for ex. E:\ D:\ X:\ )
MSDN doesn't mention anything about passing a volume guid ex. \\?\Volume{2b294683-62aa-11de-9520-806d6172696f}\ the function in my tests is more than capable of determine the type by volume GUID. Just to let you know this can save you some time if you don't need to convert first.
(Tested with Windows XP)
\\P2P\to150\
\\?\UNC\P2P\to150\
\\.\HarddiskVolume1\
allso working: complete path with GUID and mount point
\\?\Volume{cfe6df15-9df5-11de-aca4-806e6f6e6963}\Removable\SD16#1\
(Tested with Windows 7)
"To determine whether a drive is a USB-type drive, call SetupDiGetDeviceRegistryProperty and specify the SPDRP_REMOVAL_POLICY property."
This is not safe. I've seen FireWire drives with CM_REMOVAL_POLICY_EXPECT_SURPRISE_REMOVAL and USB drives with CM_REMOVAL_POLICY_EXPECT_NO_REMOVAL (a card reader in a printer).
The better way is to check the BusType for being BusTypeUsb by means of IOCTL_STORAGE_QUERY_PROPERTY.
http://msdn.microsoft.com/en-us/library/ms803642.aspx
Sample:
// szPath without trailing backslash like
// "\\\\.\\X:"
// "\\\\\?\\Volume{433619ed-c6ea-11d9-a3b2-806d6172696f}
// "\\\\.\\PhysicalDrive0"
HANDLE hDevice = CreateFile(szPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if ( hDevice != INVALID_HANDLE_VALUE ) {
DWORD dwOutBytes = 0; // IOCTL output length
STORAGE_PROPERTY_QUERY Query; // input param for query
// specify the query type
Query.PropertyId = StorageDeviceProperty;
Query.QueryType = PropertyStandardQuery;
char OutBuf[1024] = {0}; // good enough, usually about 100 bytes
PSTORAGE_DEVICE_DESCRIPTOR pDevDesc = (PSTORAGE_DEVICE_DESCRIPTOR)OutBuf;
pDevDesc->Size = sizeof(OutBuf);
// Query using IOCTL_STORAGE_QUERY_PROPERTY
BOOL res = DeviceIoControl(hDevice, // device handle
IOCTL_STORAGE_QUERY_PROPERTY, // info of device property
&Query, sizeof(STORAGE_PROPERTY_QUERY), // input data buffer
pDevDesc, pDevDesc->Size, // output data buffer
&dwOutBytes, // out's length
(LPOVERLAPPED)NULL);
CloseHandle(hDevice);
if ( res ) {
// here we are
BusType = pDevDesc->BusType;
}
}
- 6/18/2009
- Uwe Sieber
- 6/20/2009
- Thomas Lee
You could use these APIs in managed code as noted above. As an alternative to using unmanaged code, you could consider using the System.Io.DriveInfo class and its DriveType property.
For more info on this class, see http://msdn.microsoft.com/en-us/library/system.io.driveinfo_properties(VS.80).aspx
- 5/9/2009
- Thomas Lee