Being able to determine the amount of space not in use on a particular drive from C# code is very easy and straight forward. Various properties relating to a logical Windows drive can be determined by implementing the System.IO.DriveInfo class.
The DriveInfo class exposes two public properties relating to drive space not in use:
The difference between these two properties is that AvailableFreeSpace takes into account user configured disk quotas in Windows. AvailableFreeSpace therefore represents the total amount of free space available to the currently logged in user. In contrast TotalFreeSpace represents the total amount of space free on the specified drive, regardless of restrictions/quotas on the currently logged in user’s account.
To be safe use AvailableFreeSpace when doing file IO operations such as copying, downloading or creating new files.
I have written an example application that implements two extension methods which allows the calling code to specify the measurement unit (Bytes, Kilobytes, Megabytes, Gigabytes and Terabytes) in which to return TotalFreeSpace or AvailableFreeSpace.
Download the source code here
This code sample implements the DriveInfo Class’ AvailableFreeSpace and TotalFreeSpace properties and creates two extension methods allowing the calling code to specify the measurement unit (Bytes, Kilobytes, Megabytes, Gigabytes and Terabytes)
Both extension methods expects as a parameter an enum value of type DiskSizeUnit. The value will determine the unit in which AvailableFreeSpace or TotalFreeSpace gets returned.
Total Free Space
The TotalFreeSpaceFormatted extension method accepts as its first parameter an object of type DriveInfo, indicating that this method acts as an extension method to the DriveInfo Class. The second parameter is an enum instance of type DiskSizeUnit, which is used to determine the measurement unit in which to return the DriveInfo class’ TotalFreeSpace property.
The method first checks if the DriveInfo object has been instantiated (line 6). In order to convert TotalFreeSpace, which is expressed in bytes, the value is divided based on the value of the DiskSizeUnit enum parameter passed to this method. As an example if DiskSizeUnit.MegaBytes is specified, the Math.Pow() method will return 1048576 (1024 * 1024), as shown on line 9. Dividing a value expressed in bytes by 1024 and again by 1024 will return a value representing the original byte value expressed in megabytes.
Note: DriveInfo.TotalFreeSpace will return the number of bytes not in use on a particular drive. This property does not take into account limitations/restrictions on the currently logged in user such as Disk Quotas defined by Windows User Accounts.
Available Free Space
The AvailableFreeSpaceFormatted extension method accepts as its first parameter an object of type DriveInfo, indicating that this method acts as an extension method to the DriveInfo Class. The second parameter is an enum instance of type DiskSizeUnit, which is used to determine the measurement unit in which to return the DriveInfo class’ AvailableFreeSpace property.
Internally this method works almost exactly the same as the TotalFreeSpaceFormatted extension method. The only difference being checking and formatting through division the DriveInfo.AvailableFreeSpace property.
As part of this code sample a Console based test application is provided.
*I want to give credit and say a big thank you to Vitaly Zayko. All the code snippets Html markup in this article was generated using his Visual Studio Extension Code4Blog.