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