C# Program to get free space in a drive


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

Updated on: 06-Apr-2020

454 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements