简体   繁体   English

使用C#计算的可用磁盘空间与“我的电脑”中显示的值不匹配

[英]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. 我使用以下方法使用DriveInfo类来计算可用磁盘空间。 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. 以下方法返回106 gb的可用空间,而MyComputer仅显示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. 有两种约定:一种是1 kB = 1000字节,另一种是1 kB = 1024字节。 The second is also known as kibibyte . 第二个也称为kibibyte

This explains all the difference: 这解释了所有差异:
106 * 1000 * 1000 * 1000 ~= 98.8 * 1024 * 1024 * 1024. 106 * 1000 * 1000 * 1000〜= 98.8 * 1024 * 1024 * 1024。

So I think that's where the difference comes from. 所以我认为这就是差异的来源。

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

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