Situation: Your C# application accessed a computer drive with an instance of the
DriveInfo class. You noticed there are two very similar
AvailableFreeSpace. And in most cases both also return the same value. So let’s see what the difference between them is.
In this article:
In C#, we access drive information with an instance of the
DriveInfo class, which resides in the
System.IO namespace (Microsoft Docs, n.d.). A
DriveInfo object has two very similar properties, that often also return the same value:
The difference between those properties is the following (Hilyard & Teilhet, 2015):
TotalFreeSpaceproperty returns the drive’s total free space in bytes. This value ignores any drive quotas, and simply says how much total space there is. This free space is not necessarily the actual amount of space our application has access to.
AvailableFreeSpaceproperty returns the drive’s free space in bytes that’s available to the current computer user. This property takes into account any drive quotas the computer administrator set for the user account that runs our C# application. This way
AvailableFreeSpaceaccurately tells how much free space our application can access.
A disk quota is an operating system feature that allows computer administrators to control how much data each user can store on a specific drive (MSDN, n.d.; Wikipedia, 2017). This way an administrator can prevent a single user from using all the file system’s resources.
So to know all free space a drive has, we use
TotalFreeSpace. And to see which portion of disk space the current computer user can use (and by extension also our C# application the user runs), we use
Because most drives do not use drive quotas, the
AvailableFreeSpace properties often return the same value. But to accurately check the free space available, we better use the
Example: explore drive quotas in Windows
To make the above discussion more concrete, let’s explore how drive quotas work on a Windows computer. For that right-click on a drive in Windows Explorer and choose ‘Properties’. That opens a window with drive information. There select the ‘Quota’ tab to configure or view the drive’s quotas.
An example of a drive that uses disk quotas is:
Here the drive quota is set to 50 GB, with a warning for when the user takes up 40 GB of disk space. With settings like this,
AvailableFreeSpace report different values.
TotalFreeSpace property will report the drive’s total amount of free space. But the value returned by
AvailableFreeSpace will be the quote available to the current user. In this case, that returned value will match the 50 GB disk drive portion.
Now when the ‘Enable quota management’ option from that settings screen is turned off, the drive doesn’t use disk quotas. In that case both
TotalFreeSpace return the same value (Hilyard & Teilhet, 2015).
Say we don’t use disk quotas and got the following drive:
The reported free space of this C:\ drive is 112,016,179,200 bytes. Without disk quotas, both
AvailableFreeSpace return the same value. We can confirm that when we make a console application to access drive information for C:\, and then print the values of those properties:
Total free space: 112016179200 Available space: 112016179200
- In this recipe we explored the
AvailableFreeSpaceproperties from the
DriveInfoclass. See access a computer drive programmatically in C# for how to create a
DriveInfoinstance and the different properties available.
- To access the free space of a drive, that drive needs to be ready and accessible. Else our program runs into an exception and potentially crashes. See check if a drive is ready to accessfor more.
Hilyard, J. & Teilhet, S. (2015). C# 6.0 Cookbook (4th edition). Sebastopol, CA: O’Reilly Media.
Microsoft Docs (n.d.). DriveInfo Class. Retrieved on January 30, 2018, from https://docs.microsoft.com/en-us/dotnet/api/system.io.driveinfo?view=netframework-4.7.1
MSDN [Microsoft Developer Network]. Setting Disk Quotas. Retrieved on January 30, 2018, from https://msdn.microsoft.com/en-us/library/dd163561.aspx
Wikipedia (2017, October 24). Disk quota. Retrieved on January 30, 2018, from https://en.wikipedia.org/wiki/Disk_quota