MFC下写了段获取盘符的代码,现笔记如下:
采用一下函数
DWORD WINAPI GetLogicalDriveStrings( __in DWORD nBufferLength, __out LPTSTR lpBuffer );
将系统中有效的盘符以字符串的形式存在buffer中:
UINT WINAPI GetDriveType( __in LPCTSTR lpRootPathName );
确定一个盘符的类型,有removable, fixed, CD-ROM, RAM disk, or network drive.
Return code | Description |
---|---|
DRIVE_UNKNOWN | The drive type cannot be determined. |
DRIVE_NO_ROOT_DIR | The root path is invalid; for example, there is no volume is mounted at the path. |
DRIVE_REMOVABLE | The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader. |
DRIVE_FIXED | The drive has fixed media; for example, a hard drive or flash drive. |
DRIVE_REMOTE | The drive is a remote (network) drive. |
DRIVE_CDROM | The drive is a CD-ROM drive. |
DRIVE_RAMDISK | The drive is a RAM disk.
|
函数代码如下:
int CMarsTestDlg::SearchDrive()
{
UINT DriveNumber = NULL;
TCHAR DriveBuffer[100];
TCHAR *pDriveName = NULL;
DWORD DriverLength = sizeof(DriveBuffer);
int iResult = GetLogicalDriveStrings(DriverLength,(LPTSTR)DriveBuffer);
if(iResult == 0)
{
MessageBox( TEXT("获取盘符出错!"), TEXT("Intretech"), MB_OK);
return 0;
}
pDriveName = DriveBuffer;
while(*pDriveName != NULL)
{
DriveNumber = GetDriveType((LPCTSTR)pDriveName);
if(DriveNumber == DRIVE_REMOVABLE)
{
//监测到可移动盘的代码
}
pDriveName += wcslen(pDriveName) + 1;
}
return 0;
}