GetDriveType Function

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

C++
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/valueDescription
DRIVE_UNKNOWN
0

The drive type cannot be determined.

DRIVE_NO_ROOT_DIR
1

The root path is invalid; for example, there is no volume mounted at the specified path.

DRIVE_REMOVABLE
2

The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.

DRIVE_FIXED
3

The drive has fixed media; for example, a hard drive or flash drive.

DRIVE_REMOTE
4

The drive is a remote (network) drive.

DRIVE_CDROM
5

The drive is a CD-ROM drive.

DRIVE_RAMDISK
6

The drive is a RAM disk.

 

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinBase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
Unicode and ANSI namesGetDriveTypeW (Unicode) and GetDriveTypeA (ANSI)

See Also

GetDiskFreeSpace
Volume Management Functions

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Community Content

dmex
vb.net syntax
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetDriveType(ByVal lpRootPathName As String) As Integer End Function
Tags : vb.net syntax

dmex
C# syntax
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetDriveType(string drive);
Tags : c# syntax

Thomas Lee
Alternative to Unmanaged Code

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


Thomas Lee
Determinig USB

"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;
}
}



eriklitze
This seems to work with VolumeGuids

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.

Tags :

Page view tracker