C语言 获取磁盘可用空间
发布网友
发布时间:2022-04-23 13:18
我来回答
共5个回答
热心网友
时间:2023-08-17 22:03
给,在VC6上已经编译运行确认:
/*
* 获取硬盘分区序列号
* chen.brick@gmail.com 2007.5.28
*/
#include <string.h>
#include <stdio.h>
#include <windows.h>
int main() {
_ULARGE_INTEGER lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes;
DWORD serialNumber, maxComponentLength, fsFlags;
TCHAR szFileSystem[12];
TCHAR szVolumeName[24];
TCHAR szRoot[4] = "c:\\";//这里写上你需要获取的磁盘的分区
if (!GetVolumeInformation(
szRoot,
szVolumeName,
sizeof(szVolumeName),
&serialNumber,
&maxComponentLength,
&fsFlags,
szFileSystem,
sizeof(szFileSystem))) {
printf("Failed to retrieve drive information\n");
}
GetDiskFreeSpaceEx(szRoot, &lpFreeBytesAvailableToCaller, &lpTotalNumberOfBytes, &lpTotalNumberOfFreeBytes);
//根据MSDN的说明,如果分区大于2G一定要用GetDiskFreeSpaceEx
printf("卷标: %s$ %s\n分区类型: %s\n卷序列号: %04X-%04X\n",
szRoot,
szVolumeName,
szFileSystem,
serialNumber >> 16,
serialNumber & 0xFFFF
);
printf("分区总空间大小: %d MB\n",lpTotalNumberOfBytes.QuadPart / (1024 *1024));
printf("分区剩余空间大小: %d MB\n",lpTotalNumberOfFreeBytes.QuadPart / (1024 *1024));
printf("调用者可用的空间大小: %d MB\n",lpFreeBytesAvailableToCaller.QuadPart / (1024 *1024));
return(0);
}
热心网友
时间:2023-08-17 22:04
给,在VC6上已经编译运行确认:
/*
* 获取硬盘分区序列号
* chen.brick@gmail.com 2007.5.28
*/
#include <string.h>
#include <stdio.h>
#include <windows.h>
int main() {
_ULARGE_INTEGER lpFreeBytesAvailableToCaller, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes;
DWORD serialNumber, maxComponentLength, fsFlags;
TCHAR szFileSystem[12];
TCHAR szVolumeName[24];
TCHAR szRoot[4] = "c:\\";//这里写上你需要获取的磁盘的分区
if (!GetVolumeInformation(
szRoot,
szVolumeName,
sizeof(szVolumeName),
&serialNumber,
&maxComponentLength,
&fsFlags,
szFileSystem,
sizeof(szFileSystem))) {
printf("Failed to retrieve drive information\n");
}
GetDiskFreeSpaceEx(szRoot, &lpFreeBytesAvailableToCaller, &lpTotalNumberOfBytes, &lpTotalNumberOfFreeBytes);
//根据MSDN的说明,如果分区大于2G一定要用GetDiskFreeSpaceEx
printf("卷标: %s$ %s\n分区类型: %s\n卷序列号: %04X-%04X\n",szRoot,szVolumeName,szFileSystem,serialNumber >> 16,serialNumber & 0xFFFF );
printf("分区总空间大小: %d MB\n",lpTotalNumberOfBytes.QuadPart / (1024 *1024));
printf("分区剩余空间大小: %d MB\n",lpTotalNumberOfFreeBytes.QuadPart / (1024 *1024));
printf("调用者可用的空间大小: %d MB\n",lpFreeBytesAvailableToCaller.QuadPart / (1024 *1024));
return(0);
}
热心网友
时间:2023-08-17 22:04
核心代码片断:
ULARGE_INTEGER FreeBytesAvailable,TotalNumberOfBytes,TotalNumberOfFreeBytes;
GetDiskFreeSpaceEx(_T("E:"),&FreeBytesAvailable,&TotalNumberOfBytes,&TotalNumberOfFreeBytes);
热心网友
时间:2023-08-17 22:05
#include <dos.h>
#include <stdio.h>
int main(void)
{
struct dfree dfree;
getdfree(2, &dfree);
printf("%ld", (long)df_avail * df_bsec * df_sclus);
}
热心网友
时间:2023-08-17 22:05
用 GetDiskFreeSpace 函数