眾所周知,在微軟的操作系統(tǒng)下編寫應(yīng)用程序,最主要的還是通過Windows所提供的api函數(shù)來實現(xiàn)各種操作的,這些函數(shù)通常是可以直接使用的,只要包含windows.h這個頭文件。
今天我們主要介紹的是幾個常用的api函數(shù),通過它我們可以獲取用戶磁盤的相關(guān)信息。
示例程序:請點擊附件下載?! ?BR> 其主要函數(shù)原型說明如下:
1.獲取系統(tǒng)中邏輯驅(qū)動器的數(shù)量
The GetLogicalDrives function retrieves a bitmask representing the currently available disk drives.
DWORD GetLogicalDrives(void);
2.獲取所有驅(qū)動器字符串信息
The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.
DWORD GetLogicalDriveStrings(
DWORD nBufferLength,
LPTSTR lPBuffer
);
3.獲取驅(qū)動器類型
The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
UINT GetDriveType(
LPCTSTR lpRootPathName
);
4. 獲取驅(qū)動器磁盤的空間狀態(tài),函數(shù)返回的是個BOOL類型數(shù)據(jù)
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
);
以下是完整的示例程序代碼:
#include
#include
using namespace std;
int main()
{
int DiskCount = 0;
DWORD DiskInfo = GetLogicalDrives();
//利用GetLogicalDrives()函數(shù)可以獲取系統(tǒng)中邏輯驅(qū)動器的數(shù)量,函數(shù)返回的是一個32位無符號整型數(shù)據(jù)。
while(DiskInfo)//通過循環(huán)操作查看每一位數(shù)據(jù)是否為1,如果為1則磁盤為真,如果為0則磁盤不存在。
{
if(DiskInfo&1)//通過位運算的邏輯與操作,判斷是否為1
{
++DiskCount;
}
DiskInfo = DiskInfo >> 1;//通過位運算的右移操作保證每循環(huán)一次所檢查的位置向右移動一位。
//DiskInfo = DiskInfo/2;
}
cout<<"邏輯磁盤數(shù)量:"< //-------------------------------------------------------------------
今天我們主要介紹的是幾個常用的api函數(shù),通過它我們可以獲取用戶磁盤的相關(guān)信息。
示例程序:請點擊附件下載?! ?BR> 其主要函數(shù)原型說明如下:
1.獲取系統(tǒng)中邏輯驅(qū)動器的數(shù)量
The GetLogicalDrives function retrieves a bitmask representing the currently available disk drives.
DWORD GetLogicalDrives(void);
2.獲取所有驅(qū)動器字符串信息
The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system.
DWORD GetLogicalDriveStrings(
DWORD nBufferLength,
LPTSTR lPBuffer
);
3.獲取驅(qū)動器類型
The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.
UINT GetDriveType(
LPCTSTR lpRootPathName
);
4. 獲取驅(qū)動器磁盤的空間狀態(tài),函數(shù)返回的是個BOOL類型數(shù)據(jù)
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
);
以下是完整的示例程序代碼:
#include
#include
using namespace std;
int main()
{
int DiskCount = 0;
DWORD DiskInfo = GetLogicalDrives();
//利用GetLogicalDrives()函數(shù)可以獲取系統(tǒng)中邏輯驅(qū)動器的數(shù)量,函數(shù)返回的是一個32位無符號整型數(shù)據(jù)。
while(DiskInfo)//通過循環(huán)操作查看每一位數(shù)據(jù)是否為1,如果為1則磁盤為真,如果為0則磁盤不存在。
{
if(DiskInfo&1)//通過位運算的邏輯與操作,判斷是否為1
{
++DiskCount;
}
DiskInfo = DiskInfo >> 1;//通過位運算的右移操作保證每循環(huán)一次所檢查的位置向右移動一位。
//DiskInfo = DiskInfo/2;
}
cout<<"邏輯磁盤數(shù)量:"<

