Script to check available diskspace in C#/.net

Here is a script to check free diskspace in C#/.net. It uses WMI logicaldisk object to retrieve information on the system drives.

.NET provides ManagementObject class to encapsulate the Windows WMI interface. Here is the sample code:


ManagementObject Disk = new ManagementObject("win32_logicaldisk.deviceid="c:"");
Disk.Get();
if (double.Parse(Disk["FreeSpace"].ToString()) / 1024 / 1024 / 1024 < 10)
// do something here

The above code gets a reference to the “C” drive and then checks if the free space is less than 10GB. The WMI API returns the space in bytes, hence, conversion to GB is required.

Leave a Reply

Your email address will not be published. Required fields are marked *