磁碟容量(GetDiskFreeSpace函式和GetDiskFreeSpaceEx函式)
阿新 • • 發佈:2019-02-07
GetDiskFreeSpace函式和GetDiskFreeSpaceEx函式,主要是用來計算磁碟的各種容量,具體能計算什麼,程式碼中註釋得很詳細
<pre name="code" class="cpp">#include <Windows.h> #include <iostream> using namespace std; int main() { //得出磁碟的可用空間 DWORD dwTotalClusters;//總的簇 DWORD dwFreeClusters;//可用的簇 DWORD dwSectPerClust;//每個簇有多少個扇區 DWORD dwBytesPerSect;//每個扇區有多少個位元組 BOOL bResult = GetDiskFreeSpace(TEXT("C:"),&dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters); if(bResult){ cout << "使用GetDiskFreeSpace函式獲取磁碟空間資訊" << endl; cout << "總簇數量: " << dwTotalClusters << endl; cout << "可用的簇: " << dwFreeClusters << endl; cout << "每個簇有多少個扇區: " << dwSectPerClust << endl; cout << "每個扇區有多少個位元組: " << dwBytesPerSect << endl; cout << "磁碟總容量: " << dwTotalClusters * (DWORD64)dwSectPerClust * (DWORD64)dwBytesPerSect << endl; cout << "磁碟空閒容量: " << dwFreeClusters * (DWORD64)dwSectPerClust * (DWORD64)dwBytesPerSect << endl; } cout << "\n\n" << endl; DWORD64 qwFreeBytes, qwFreeBytesToCaller, qwTotalBytes; bResult = GetDiskFreeSpaceEx(TEXT("C:"), (PULARGE_INTEGER)&qwFreeBytesToCaller, (PULARGE_INTEGER)&qwTotalBytes, (PULARGE_INTEGER)&qwFreeBytes); if(bResult){ cout << "使用GetDiskFreeSpaceEx函式獲取磁碟空間資訊" << endl; cout << "磁碟總容量: " << qwTotalBytes << endl; cout << "可用的磁碟空閒容量: " << qwFreeBytes << endl; cout << "磁碟空閒容量: " << qwFreeBytesToCaller << endl; } system("pause"); }
執行結果