简体   繁体   English

WinAPI。 如何获得硬盘空闲/忙碌空间

[英]WinAPI. How to get Hard Drive free/busy space

I'm writing a program for getting some of the hard drive information.我正在编写一个程序来获取一些硬盘信息。 At the moment, i was able to get the full size of the hard disk using the DeviceIoControl function and corresponding IOCTL_DISK_GET_DRIVE_GEOMETRY flag .目前,我能够使用DeviceIoControl函数和相应的IOCTL_DISK_GET_DRIVE_GEOMETRY标志获得硬盘的完整大小。

I'm tried to use GetDiskFreeSpace function and send the "\\\\.\\PhysicalDrive0" argument, but it doesn't work.我试图使用GetDiskFreeSpace函数并发送“\\\\.\\PhysicalDrive0”参数,但它不起作用。

BOOL bResult = GetDiskFreeSpace("\\\\.\\PhysicalDrive0", &dwSectorsPerCluster,
                   &dwBytesPerSector, &dwNumberOfFreeClusters,
                   &dwTotalNumberOfClusters);

if (bResult == FALSE) {
  std::cout << "Can't retrieve disk free space info." << std::endl;
  return bResult;
}

Output: Can't retrieve disk free space info.输出:无法检索磁盘可用空间信息。 For example, "\\\\.\\PhysicalDrive0" replaced on "C:" work fine.例如,在“C:”上替换“\\\\.\\PhysicalDrive0”工作正常。

The question is how to get the free or used space on hard drive using WinAPI functions?问题是如何使用 WinAPI 函数获取硬盘上的可用空间或已用空间?

PS Without using WMI. PS 不使用 WMI。

GetDiskFreeSpace does "work", but you need to use the correct first parameter. GetDiskFreeSpace确实“有效”,但您需要使用正确的第一个参数。

lpRootPathName [in]

The root directory of the disk for which information is to be returned.要返回信息的磁盘的根目录。

"\\\\\\\\.\\\\PhysicalDrive0" is incorrect, because this is for the whole disk drive and it is not mounted by file system. "\\\\\\\\.\\\\PhysicalDrive0"是不正确的,因为这是针对整个磁盘驱动器的,它不是由文件系统挂载的。 However, this request is handled by file system.但是,此请求由文件系统处理。 You need to use names like "\\\\\\\\?\\\\c:\\\\" or "\\\\\\\\?\\\\Harddisk0Partition<N>\\\\" or "\\\\\\\\?\\\\HarddiskVolume<N>\\\\" or "\\\\\\\\?\\\\Volume{guid}\\\\" - slash on the end is very important - without it will not work.您需要使用诸如"\\\\\\\\?\\\\c:\\\\""\\\\\\\\?\\\\Harddisk0Partition<N>\\\\""\\\\\\\\?\\\\HarddiskVolume<N>\\\\""\\\\\\\\?\\\\Volume{guid}\\\\" - 末尾的斜杠非常重要 - 没有它就无法工作。 But in case of "\\\\\\\\?\\\\PhysicalDrive0\\\\" - slash will not help.但在"\\\\\\\\?\\\\PhysicalDrive0\\\\" - 斜线将无济于事。 It will not work, because no FS is mounted on this device.它不会工作,因为此设备上未安装 FS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM