0

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;
    }
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • You probably want to show the code you use to calculate the long to gigabyte as well. Also, haven't you considered what happens if you have more than one disk? – Claus Jørgensen Jul 10 '16 at 14:34

1 Answers1

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.

Desirius
  • 220
  • 1
  • 8