To get the free space in a drive, use the AvailableFreeSpace and TotalFreeSpace properties in C#.
Firstly, set the name of the drive using DriveInfo −
DriveInfo dInfo = new DriveInfo("D");Let’s say, you need to find the available space for D drive −
Example
using System;
using System.Linq;
using System.IO;
public class Demo {
public static void Main() {
DriveInfo dInfo = new DriveInfo("D");
// Get free space
Console.WriteLine(dInfo.AvailableFreeSpace);
// get total free space
Console.WriteLine(dInfo.TotalFreeSpace);
}
}Output
The following is the output showing the free space available in D drive−
722243567912 722243567912