简体   繁体   中英

Free disk space calculated using C# doesn't match the value shown in My Computer

I used the following method to calculate free disk space using DriveInfo class. But it doesn't match the free disk space value shown in My Computer. Following method returns 106 gb of free space while MyComputer only shows a free space of 98.8 GB. How can I calculate the accurate value? Why is there a difference?

public long GetTotalFreeSpace(string driveName)
    {
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            if (drive.IsReady && drive.Name == driveName)
            {
                return drive.TotalFreeSpace;
            }
        }
        return -1;
    }

There are two conventions: One is that 1 kB = 1000 bytes and the other is that 1 kB = 1024 bytes. The second is also known as kibibyte .

This explains all the difference:
106 * 1000 * 1000 * 1000 ~= 98.8 * 1024 * 1024 * 1024.

So I think that's where the difference comes from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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