磁盘容量(GetDiskFreeSpace函数和GetDiskFreeSpaceEx函数)

来源:互联网 发布:知乎怎么刷新 编辑:程序博客网 时间:2024/06/17 02:27

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");}



运行结果


0 0
原创粉丝点击