使用的API
GetDiskFreeSpace和GetDiskFreeSpaceEx
GetDiskFreeSpace使用
代码
#include <Windows.h>
#include <stdio.h>
int main() {
BOOL bResult;
DWORD dwTotalClusters; // 总的簇数
DWORD dwFreeClusters; // 可用的簇大小
DWORD dwSectPerClust; // 每一个簇有多少个扇区
DWORD dwBytesPerSect; // 每一个扇区有多少个字节
bResult = GetDiskFreeSpace(
TEXT("C:"),
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters
);
if (bResult) {
printf("总簇数量: \t\t\t%d\n", dwTotalClusters);
printf("空闲的簇数量: \t\t\t%d\n", dwFreeClusters);
printf("每簇的扇区数量:\t\t\t%d\n", dwSectPerClust);
printf("每扇区的字节数:\t\t\t%d\n", dwBytesPerSect);
printf("磁盘总容量(字节):\t\t\t%I64d\n", (DWORD64)dwTotalClusters * (DWORD64)dwSectPerClust * (DWORD64)dwBytesPerSect);
printf("空闲的空间容量(字节):\t\t\t%I64d\n", (DWORD64)dwFreeClusters * (DWORD64)dwSectPerClust * (DWORD64)dwBytesPerSect);
}
return 0;
}
运行效果
使用GetDiskFreeSpaceEx函数
使用该函数我们不用自己计算容量大小了,函数已经计算好了
代码
#include <Windows.h>
#include <stdio.h>
int main() {
BOOL bResult;
DWORD dwTotalClusters; // 总的簇数
DWORD dwFreeClusters; // 可用的簇大小
DWORD dwSectPerClust; // 每一个簇有多少个扇区
DWORD dwBytesPerSect; // 每一个扇区有多少个字节
bResult = GetDiskFreeSpace(
TEXT("C:"),
&dwSectPerClust,
&dwBytesPerSect,
&dwFreeClusters,
&dwTotalClusters
);
if (bResult) {
printf("使用GetDiskFreeSpace获取磁盘信息\n");
printf("总簇数量: \t\t\t%d\n", dwTotalClusters);
printf("空闲的簇数量: \t\t\t%d\n", dwFreeClusters);
printf("每簇的扇区数量:\t\t\t%d\n", dwSectPerClust);
printf("每扇区的字节数:\t\t\t%d\n", dwBytesPerSect);
printf("磁盘总容量(字节):\t\t\t%I64d\n", (DWORD64)dwTotalClusters * (DWORD64)dwSectPerClust * (DWORD64)dwBytesPerSect);
printf("空闲的空间容量(字节):\t\t\t%I64d\n", (DWORD64)dwFreeClusters * (DWORD64)dwSectPerClust * (DWORD64)dwBytesPerSect);
}
printf("\n\n");
DWORD64 qwFreeBytes; // 可用空闲的空间容量
DWORD64 qwFreeBytesToCaller; // 总的空闲的空间容量, Windows有可能会预先预留一部分空间
DWORD64 qwTotalBytes; // 总的空间
bResult = GetDiskFreeSpaceEx(TEXT("C:"), (PULARGE_INTEGER)& qwFreeBytesToCaller, (PULARGE_INTEGER)& qwTotalBytes, (PULARGE_INTEGER)& qwFreeBytes);
if (bResult) {
printf("使用GetDiskFreeSpaceEx获取磁盘信息\n");
printf("磁盘总容量(字节):\t\t\t%I64d\n", qwTotalBytes);
printf("可用空闲的空间容量(字节):\t\t\t%I64d\n", qwFreeBytes);
printf("总的空闲的空间容量(字节):\t\t\t%I64d\n", qwFreeBytesToCaller);
}
return 0;
}