GetDiskFreeSpaceEx reports wrong number of free bytes

百般思念 提交于 2019-11-29 15:25:26

I have a single-user machine with no disk quota in operation. I posted your code into a dialog based MFC application and ran it, with the single proviso that I used "C:\" as the lpDirectoryName parameter so I could compare against the drive free space as reported by the system. That seemed logical as free space is only meaningful for a drive, not a folder.

At first I thought that I was seeing a similar problem, but then I re-ran the check (I tied it to a button), and got the same result as the properties dialog at that moment. It seems the free space on a drive is a fairly dynamic quantity - this is not terribly suprising if it is the system drive - and even absent the criteria that other posters have quite correctly reported, you may not see precisely the same number as the properties dialog reports at the moment it was run.

Quoting (with editing) the documentation for GetDiskFreeSpaceEx, emphasis mine:

lpFreeBytesAvailable [out, optional]-

A pointer to a variable that receives the total number of free bytes on a disk that are available to the user who is associated with the calling thread.

This parameter can be NULL.

If per-user quotas are being used, this value may be less than the total number of free bytes on a disk.

lpTotalNumberOfBytes [out, optional]-

A pointer to a variable that receives the total number of bytes on a disk that are available to the user who is associated with the calling thread.

This parameter can be NULL.

If per-user quotas are being used, this value may be less than the total number of bytes on a disk.

To determine the total number of bytes on a disk or volume, use IOCTL_DISK_GET_LENGTH_INFO.

In other words, this number depends on the user, and if you want to match the value returned by Explorer, use lpFreeBytesAvailable.

One possibility come to mind. perhaps one is not taking into account space lost to partitioning (windows typically leaves 8MB at the end of the drive as slack). Basically, there is a difference between the space left on the physical drive and the logical one represented by the partition. Or space lost to the filesystem itself.

I couldn't say if these are actually the case, but I'd look into it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!