眾所周知,在微軟的操作系統下編寫應用程序,最主要的還是通過windows所提供的api函數來實現各種操作的,這些函數通常是可以直接使用的,只要包含windows.h這個頭文件?! 〗裉煳覀冎饕榻B的是幾個常用的api函數,通過它我們可以獲取用戶磁盤的相關信息?! ∈纠绦颍赫堻c擊附件下載。 其主要函數原型說明如下: 1.獲取系統中邏輯驅動器的數量The GetLogicalDrives function retrieves a bitmask rePResenting the currently available disk drives.DWord GetLogicalDrives(void); 2.獲取所有驅動器字符串信息The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.DWORD GetLogicalDriveStrings( DWORD nBufferLength, LPTSTR lpBuffer ); 3.獲取驅動器類型The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive. UINT GetDriveType( LPCTSTR lpRootPathName ); 4. 獲取驅動器磁盤的空間狀態,函數返回的是個BOOL類型數據The GetDiskFreeSpaceEx function retrieves information about the amount of space available on a disk volume: the total amount of space, the total amount of free space, and the total amount of free space available to the user associated with the calling thread.BOOL GetDiskFreeSpaceEx( LPCTSTR lpDirectoryName, PULARGE_INTEGER lpFreeBytesAvailable, PULARGE_INTEGER lpTotalNumberOfBytes, PULARGE_INTEGER lpTotalNumberOfFreeBytes ); 以下是完整的示例程序代碼: 更多內容請看C/C++技術專題專題,或 #include <iostream> #include <windows.h> using namespace std;
int main() { int DiskCount = 0; DWORD DiSKINfo = GetLogicalDrives(); //利用GetLogicalDrives()函數可以獲取系統中邏輯驅動器的數量,函數返回的是一個32位無符號整型數據。 while(DiskInfo)//通過循環操作查看每一位數據是否為1,假如為1則磁盤為真,假如為0則磁盤不存在。 { if(DiskInfo&1)//通過位運算的邏輯與操作,判定是否為1 { ++DiskCount;