General free space detection not working on large hard drives?

I use the AC # program to find out how many GB are still free on the hard drive (total size 1 TB, free size 110 GB (more precisely, according to the properties of the drive in Windows Explorer: 118.333.329.408 bytes) according to Windows) .

My problem is that the result I get is disabled.

It is 10,135,252,992 Bytes according to the C # method that I use below ..... but according to Windows 110! GB are free.

Note. I am talking about Windows Server here, and the drive is drive D. Thus, there is no paging file on it, and no hidden system files (at least no more than any non-system drive, since the system drive is drive C).

 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 this be and how to fix it?

+7
c # io
source share
3 answers

Under the hood, the System.IO.DriveInfo assembly uses the GetDiskFreeSpaceEx function from the Windows API.

The difference between the standard version of GetDiskFreeSpace and GetDiskFreeSpaceEx is as follows:

GetDiskFreeSpace

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

GetDiskFreeSpaceEx

GetDiskFreeSpaceEx function Retrieves information about the amount of space that is available on the disk volume, which represents 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 stream.

Most likely, you click some kind of quota in the space available for the current user.

+8
source share

By running it several times, not unexpectedly, there are noticeable, but slight fluctuations in numbers. In most cases, the Win32 API is consistent with Windows Explorer with a byte (see below). This is for a regular Windows 10 laptop, not a server. In the case of the server, I think there may be processes causing quite large fluctuations.

enter image description here

 Sectors per Cluster: 8 Bytes Per Sector: 512 Number of Free Clusters: 126,523,800 Total Number Of Clusters: 239,423,046 Free Bytes Available: 518,241,484,800 Total Number Of Free Bytes: 518,241,484,800 Total Number Of Bytes: 980,676,796,416 

Try the following:

 static void Main(string[] args) { string drive = "C:"; var diskSpace = DiskHelperWin32.GetDiskFreeSpace(drive); var diskSpaceEx = DiskHelperWin32.GetDiskFreeSpaceEx(drive); Console.WriteLine("Sectors per Cluster: {0}", diskSpace.SectorsPerCluster); Console.WriteLine("Bytes Per Sector: {0}", diskSpace.BytesPerSector); Console.WriteLine("Number of Free Clusters: {0:0,0}", diskSpace.NumberOfFreeClusters); Console.WriteLine("Total Number Of Clusters: {0:0,0}", diskSpace.TotalNumberOfClusters); Console.WriteLine(); Console.WriteLine("Free Bytes Available: {0:0,0}", diskSpaceEx.FreeBytesAvailable); Console.WriteLine("Total Number Of Free Bytes: {0:0,0}", diskSpaceEx.TotalNumberOfFreeBytes); Console.WriteLine("Total Number Of Bytes: {0:0,0}", diskSpaceEx.TotalNumberOfBytes); Console.ReadLine(); } 

DiskHelperWin32:

 public static class DiskHelperWin32 { [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetDiskFreeSpace(string lpRootPathName, out uint lpSectorsPerCluster, out uint lpBytePerSector, out uint lpNumberOfFreeClusters, out uint lpTotalNumberOfClusters); public static DiskFreeSpace GetDiskFreeSpace(string lpRootPathName) { uint SectorsPerCluster; uint BytesPerSector; uint NumberOfFreeClusters; uint TotalNumberOfClusters; bool success = GetDiskFreeSpace(lpRootPathName, out SectorsPerCluster, out BytesPerSector, out NumberOfFreeClusters, out TotalNumberOfClusters); if (!success) throw new System.ComponentModel.Win32Exception(); return new DiskFreeSpace { BytesPerSector = BytesPerSector, SectorsPerCluster = SectorsPerCluster, NumberOfFreeClusters = NumberOfFreeClusters, TotalNumberOfClusters = TotalNumberOfClusters }; } [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes); public static DiskFreeSpaceEx GetDiskFreeSpaceEx(string lpDirectoryName) { ulong FreeBytesAvailable; ulong TotalNumberOfBytes; ulong TotalNumberOfFreeBytes; bool success = GetDiskFreeSpaceEx(lpDirectoryName, out FreeBytesAvailable, out TotalNumberOfBytes, out TotalNumberOfFreeBytes); if (!success) throw new System.ComponentModel.Win32Exception(); return new DiskFreeSpaceEx { FreeBytesAvailable = FreeBytesAvailable, TotalNumberOfFreeBytes = TotalNumberOfFreeBytes, TotalNumberOfBytes = TotalNumberOfBytes }; } } 

Model:

 public class DiskFreeSpace { public uint SectorsPerCluster { get; set; } public uint BytesPerSector { get; set; } public uint NumberOfFreeClusters { get; set; } public uint TotalNumberOfClusters { get; set; } } public class DiskFreeSpaceEx { public ulong FreeBytesAvailable { get; set; } public ulong TotalNumberOfBytes { get; set; } public ulong TotalNumberOfFreeBytes { get; set; } } 

EDIT: Reply to the comments section.

I doubt the β€œproblem” is related to any of the APIs or whether the vm server is. These numbers fluctuate even on a regular computer. For example, think of the Disk Defragmenter that appears and appears. On the server, these fluctuations can be significant, depending on what works on it. Also note the differences between FreeBytesAvailable and TotalNumberOfFreeBytes . In our case, this is a perfect match, but obviously not the rule.

Please run our MCVE sample application on your server and return with the numbers. Post them as an addition to the body of your question.

+2
source share

The answer is not wrong. 118333329408/2 ^ 30 = 110.206501007

Remember that 1 KB is not 1000 bytes, but 1024 bytes. 1 GB - 1024 * 1024 * 1024 bytes.

Actually starts to change when numbers get big.

+1
source share

All Articles