C++ Windows API獲取驅動器根路徑和屬性(二)
阿新 • • 發佈:2019-01-05
#include<Windows.h> #include<stdlib.h> #include<iostream> const int BUFSIZE = MAX_PATH; BOOL GetDirverInfo(LPSTR szDrive); int main(){ TCHAR buf[BUFSIZE]; //卷標資訊 HANDLE hVol; //卷遍歷控制代碼 BOOL bFlag; hVol = FindFirstVolume(buf, BUFSIZE);//查詢主機中的第一個驅動器,並返回驅動器裝置名 //第一個引數是指向驅動器名的記憶體緩衝區,第二個引數是第一個引數指向緩衝區的大小,單位是位元組 if (hVol == INVALID_HANDLE_VALUE) { std::cout << "No volumes found\n"; return -1; } GetDirverInfo(buf); while (FindNextVolume(hVol, buf, BUFSIZE)) {//查詢主機中後繼的邏輯驅動器,返回驅動器裝置名,成功則返回True //第一個引數是FindFirstVolume所返回的驅動器查詢控制代碼,第二個引數是指向儲存驅動器名的記憶體緩衝區,第三個 //引數是第二個引數所指緩衝區的大小,以位元組為單位 GetDirverInfo(buf); } bFlag = FindVolumeClose(hVol);//關閉FindFirstVolume開啟的卷遍歷控制代碼,返回BOOL值標識是否成功關閉控制代碼 int i; std::cin >> i; return (bFlag); } /* ************************************************* *功能:獲取驅動器的屬性 *引數:LPSTR szDrive * 指明要獲取屬性的驅動器的很路徑,如D:\ *返回值BOOL:表示是否成功 ***************************************************/ BOOL GetDirverInfo(LPSTR szDrive) { UINT uDriveType; //驅動器型別 DWORD dwVolumeSerialNumber; //卷序列號 DWORD dwMaximumComponentLength; //最大元件長度 DWORD dwFileSystemFlags; //檔案系統標識 CHAR szFileSystemNameBuffer[BUFSIZE]; //檔案系統名稱緩衝 CHAR szDirverName[MAX_PATH]; //驅動器名稱 //最大字元長度的路徑。 std::cout << "\n" << szDrive << std::endl; uDriveType = GetDriveType(szDrive); //獲取驅動器的物理型別 該函式返回驅動器型別 switch (uDriveType) { case DRIVE_UNKNOWN: //驅動器型別不確定 std::cout << "The drive type cannot be determined."; break; case DRIVE_NO_ROOT_DIR: //根目錄無效, std::cout << "The root path is invalid,for example,no volume is mounted at the path"; break; case DRIVE_REMOVABLE: //可移動裝置, 比如U盤 std::cout << "The drive is a type that has removable media,for example,a floppy drive or removable hard disk."; break; case DRIVE_FIXED: //固定的驅動;例如,一個硬碟或快閃記憶體驅動器 std::cout << "The drive is a type that cannot be removed,for example,a fixed hard drive."; break; case DRIVE_REMOTE://遠端(網路)驅動器 std::cout << "The drive is a remote (network) drive."; break; case DRIVE_CDROM: //cd - rom驅動器 std::cout << "The drive is a CD-ROM drive."; break; case DRIVE_RAMDISK: //RAM磁碟 std::cout << "The drive is a RAM disk."; break; } /****************************************************************************** *函式:GetVolumeInformation() *引數: * _In_opt_ LPCTSTR lpRootPathName,磁碟驅動器程式碼字串 * _Out_opt_ LPTSTR lpVolumeNameBuffer,磁碟驅動器卷標名稱 * _In_ DWORD nVolumeNameSize,磁碟驅動器卷標名稱長度 * _Out_opt_ LPDWORD lpVolumeSerialNumber,磁碟驅動器卷標序列號 * _Out_opt_ LPDWORD lpMaximumComponentLength,系統允許的最大檔名長度 * _Out_opt_ LPDWORD lpFileSystemFlags, 檔案系統標識 * _Out_opt_ LPTSTR lpFileSystemNameBuffer, 檔案作業系統名稱 * _In_ DWORD nFileSystemNameSize 檔案作業系統名稱長度 *********************************************************************************/ if (!GetVolumeInformation(szDrive, szDirverName, MAX_PATH, &dwVolumeSerialNumber, &dwMaximumComponentLength, &dwFileSystemFlags ,szFileSystemNameBuffer, BUFSIZE)) { //獲取驅動器的相關屬性 return FALSE; } if (0 != lstrlen(szDirverName)) { //確定指定的字串的長度(不包括終止null字元)。 std::cout << "\nDrive Name is" << szDirverName; } std::cout << "\nVolume Serial Number is " << dwVolumeSerialNumber << std::endl; std::cout << "Maximum Component Length is " << dwMaximumComponentLength << std::endl; std::cout << "System Type is " << szFileSystemNameBuffer << std::endl; if (dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS) { //指定的卷支援重新解析 std::cout << "The file system does not support volume mount points.\n"; } if (dwFileSystemFlags & FILE_VOLUME_QUOTAS) { //指定的卷支援磁碟配額 std::cout << "The file system supports disk quotas.\n"; } if (dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH) { //指定的卷支援區分檔案的大小寫 std::cout << "The file system supports cass-sensitive file name.\n"; } std::cout << "..........................................................................\n"; return TRUE; }