Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
File Services
File Systems
Volume Management
 GetDriveType Function

  Switch on low bandwidth view
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: 7/9/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
vb.net syntax      dmex   |   Edit   |   Show History
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function GetDriveType(ByVal lpRootPathName As String) As Integer End Function
Flag as ContentBug
C# syntax      dmex   |   Edit   |   Show History
[DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern int GetDriveType(string drive);
Tags What's this?: c# (x) syntax (x) Add a tag
Flag as ContentBug
Alternative to Unmanaged Code      Thomas Lee   |   Edit   |   Show History

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

Determinig USB      Uwe Sieber ... Thomas Lee   |   Edit   |   Show History

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


Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker