眾所周知,在微軟的作業系統下編寫應用程式,最主要的還是通過windows所提供的api函數來實現各種操作的,這些函數通常是可以直接使用的,只要包含windows.h這個標頭檔, 下載源檔案
今天我們主要介紹的是幾個常用的api函數,通過它我們可以擷取使用者磁碟的相關資訊。
其主要函數原型說明如下:
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
);
以下是完整的樣本程式碼
//程式作者:管寧
//網站:www.cndev-lab.com
//所有稿件均有著作權,如要轉載,請務必注名出處和作者
#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;
}
DiskInfo = DiskInfo >> 1;//通過位元運算的右移操作保證每迴圈一次所檢查的位置向右移動一位。
//DiskInfo = DiskInfo/2;
}
cout<<"邏輯磁碟數量:"<<DiskCount<<endl;
//-------------------------------------------------------------------
int DSLength = GetLogicalDriveStrings(0,NULL);
//通過GetLogicalDriveStrings()函數擷取所有磁碟機字串資訊長度。
char* DStr = new char[DSLength];//用擷取的長度在堆區建立一個c風格的字串數組
GetLogicalDriveStrings(DSLength,(LPTSTR)DStr);
//通過GetLogicalDriveStrings將字串資訊複製到堆區數組中,其中儲存了所有磁碟機的資訊。
int DType;
int si=0;
BOOL fResult;
unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes;
for(int i=0;i<DSLength/4;++i)
//為了顯示每個磁碟機的狀態,則通過迴圈輸出實現,由於DStr內部儲存的資料是A:\NULLB:\NULLC:\NULL,這樣的資訊,所以DSLength/4可以獲得具體大迴圈範圍
{
char dir[3]={DStr[si],':','\\'};
cout<<dir;
DType = GetDriveType(DStr+i*4);
//GetDriveType函數,可以擷取磁碟機類型,參數為磁碟機的根目錄
if(DType == DRIVE_FIXED)
{
cout<<"硬碟";
}
else if(DType == DRIVE_CDROM)
{
cout<<"光碟機";
}
else if(DType == DRIVE_REMOVABLE)
{
cout<<"可移動式磁碟";
}
else if(DType == DRIVE_REMOTE)
{
cout<<"網路磁碟";
}
else if(DType == DRIVE_RAMDISK)
{
cout<<"虛擬RAM磁碟";
}
else if (DType == DRIVE_UNKNOWN)
{
cout<<"未知裝置";
}
fResult = GetDiskFreeSpaceEx (
dir,
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
//GetDiskFreeSpaceEx函數,可以擷取磁碟機磁碟的空間狀態,函數返回的是個BOOL類型資料
if(fResult)//通過返回的BOOL資料判斷磁碟機是否在工作狀態
{
cout<<" totalspace:"<<(float)i64TotalBytes/1024/1024<<" MB";//磁碟總容量
cout<<" freespace:"<<(float)i64FreeBytesToCaller/1024/1024<<" MB";//磁碟剩餘空間
}
else
{
cout<<" 裝置未準備好";
}
cout<<endl;
si+=4;
}