5

I'm using a c# program to see how many GB are still free on a hard drive (total size 1 TB, free size 110 GB (to be exact, according to drive properties in Windows explorer: 118.333.329.408 Bytes) according to Windows).

My problem is that the result I'm getting is.....off.

It's 10,135,252,992 Bytes according to the C# method I'm using below.....but according to Windows 110! GB are free.

Note: I'm talking about a Windows Server here and the drive is the D drive. So no swap file on it, and no hidden System files (at least not more than any non System drive has as the System drive is the C drive).

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

My question here is how can that be and how to correct it?

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • 1
    @TimSchmelter But we're talking about 110GB, OP's result is only 10% of that. – René Vogt Dec 15 '17 at 10:27
  • @TimSchmelter as Rene mentioned my Problem is that the hard drive should have 110 GB free space. – Thomas Dec 15 '17 at 10:30
  • @RenéVogt I updated my qquestion a bit to accomodate it and also to add what drive property in Windows Explorer shows – Thomas Dec 15 '17 at 10:32
  • Is this server virtualized? Is the D: drive shared with other virtual machines? Maybe there is some sort of quota. – John Wu Dec 15 '17 at 10:35
  • What value does `.AvailableFreeSpace` give? - Oh wait, I think I've got myself muddled and `TotalFreeSpace` is the one that pays attention to quotas already. – Equalsk Dec 15 '17 at 10:38
  • @Equalsk 10,135,252,992 is the result – Thomas Dec 15 '17 at 10:40
  • @JohnWu Yes ist a virtual Server and I'm running the application from within the virtual Server. If you mean if D is on a physical drive shared among other VMs then yes very probable. If you mean if D is shared from within the Server with other VMs then no. – Thomas Dec 15 '17 at 10:41
  • Are you 100% sure that you are using `TotalFreeSpace` property and not `AvailableFreeSpace` or `TotalSize`? Can you show us all three of them? – Leonid Vasilev Dec 22 '17 at 14:30
  • I tried your code on my machine (physical), it shows the correct available size. As said by @JohnWu this may be caused by the fact that it's a virtual machine, and file explorer is reading from a different API than DriveInfo object – Majid ALSarra Dec 25 '17 at 05:21
  • Is there a swap file on that partition? Perhaps some other hidden "internal" windows (or even just that app-state/"roaming") data? I am not totally sure on this one, but I suspect Windows may not count those "freeable" resources that it dynamically manages, while your method gets the direct NTFS-reported numbers.. – George Shapovalov Dec 15 '17 at 10:28
  • I fear this should be a comment not an answer ,) that aside: It's the D drive so no swap file on it and no "normal" hidden things from the OS as ist not the System drive. – Thomas Dec 15 '17 at 10:30
  • @LeonidVasilyev current Status (Explorer: 106 GB free, Total free: 108.958 GB, available free: 108.958 GB, total space: 614.396 GB). What I don't get is why it now has a similar amount all of a sudden as I didn't Change a Thing in the Version I ran there (but before I got the strange amount the whole time). – Thomas Dec 28 '17 at 10:00

1 Answers1

8

Under the hood, the System.IO.DriveInfo assembly appears to use the GetDiskFreeSpaceEx function from the Windows API.

The difference between the standard version of GetDiskFreeSpace and GetDiskFreeSpaceEx comes down to this:

GetDiskFreeSpace:

GetDiskFreeSpace function Retrieves information about the specified disk, including the amount of free space on the disk.

GetDiskFreeSpaceEx:

GetDiskFreeSpaceEx function Retrieves information about the amount of space that is available on a disk volume, which is the total amount of space, the total amount of free space, and the total amount of free space available to the user that is associated with the calling thread.

Most likely, you're hitting some quota on the space available to the current user.

dperish
  • 1,493
  • 16
  • 27
  • interesting is there a way to use the other Version? (thus GetDiskFreeSpace) – Thomas Dec 19 '17 at 14:05
  • 1
    Yep, you'll have to `DllImport` the module and mark it as a static external method. This answer shows that technique: https://stackoverflow.com/a/27220546/2147110 – dperish Dec 19 '17 at 14:17
  • This is wrong. `GetDiskFreeSpace()` is obsolete. It doesn't work for disks larger than 2 GB. `DriveInfo.TotalFreeSpace` property uses `GetDiskFreeSpaceEx()` third output argument which is `lpTotalNumberOfFreeBytes`. And according to [documentation](https://msdn.microsoft.com/en-us/library/system.io.driveinfo.totalfreespace.aspx) it doesn't take disk quotas into account. – Leonid Vasilev Dec 22 '17 at 14:37