Find free disk space using C#

The DriveInfo class in C# provides information about drives and their storage capacity. You can use this class to find free disk space, total disk space, and calculate the percentage of available space on any drive.

Syntax

Following is the syntax for creating a DriveInfo instance −

DriveInfo driveInfo = new DriveInfo("driveLetter");

Following are the key properties for disk space information −

long availableSpace = driveInfo.AvailableFreeSpace;
long totalSpace = driveInfo.TotalSize;
long usedSpace = driveInfo.TotalSize - driveInfo.AvailableFreeSpace;

Using DriveInfo to Get Free Disk Space

Example

using System;
using System.IO;

class Program {
   public static void Main() {
      DriveInfo dInfo = new DriveInfo("C");
      
      Console.WriteLine("Drive: " + dInfo.Name);
      Console.WriteLine("Total Size: " + (dInfo.TotalSize / (1024 * 1024 * 1024)) + " GB");
      Console.WriteLine("Available Free Space: " + (dInfo.AvailableFreeSpace / (1024 * 1024 * 1024)) + " GB");
      
      double percentage = (dInfo.AvailableFreeSpace / (double)dInfo.TotalSize) * 100;
      Console.WriteLine("Free Space Percentage: {0:0.00}%", percentage);
   }
}

The output of the above code is −

Drive: C:\
Total Size: 465 GB
Available Free Space: 123 GB
Free Space Percentage: 26.45%

Using DriveInfo to Check All Available Drives

Example

using System;
using System.IO;

class Program {
   public static void Main() {
      DriveInfo[] drives = DriveInfo.GetDrives();
      
      foreach (DriveInfo drive in drives) {
         if (drive.IsReady) {
            Console.WriteLine("Drive: " + drive.Name);
            Console.WriteLine("Type: " + drive.DriveType);
            
            long totalGB = drive.TotalSize / (1024 * 1024 * 1024);
            long freeGB = drive.AvailableFreeSpace / (1024 * 1024 * 1024);
            double freePercentage = (drive.AvailableFreeSpace / (double)drive.TotalSize) * 100;
            
            Console.WriteLine("Total: {0} GB, Free: {1} GB ({2:0.00}%)", totalGB, freeGB, freePercentage);
            Console.WriteLine("------------------------");
         }
      }
   }
}

The output of the above code is −

Drive: C:\
Type: Fixed
Total: 465 GB, Free: 123 GB (26.45%)
------------------------
Drive: D:\
Type: Fixed
Total: 931 GB, Free: 456 GB (48.98%)
------------------------

Key Properties of DriveInfo

Property Description
AvailableFreeSpace Gets the amount of available free space on the drive in bytes
TotalFreeSpace Gets the total amount of free space available on the drive in bytes
TotalSize Gets the total size of storage space on the drive in bytes
IsReady Gets a value indicating whether the drive is ready
DriveType Gets the drive type (Fixed, Removable, Network, etc.)

Checking Drive Availability Before Access

Example

using System;
using System.IO;

class Program {
   public static void Main() {
      try {
         DriveInfo dInfo = new DriveInfo("E");
         
         if (dInfo.IsReady) {
            long freeSpaceMB = dInfo.AvailableFreeSpace / (1024 * 1024);
            Console.WriteLine("Drive E: is ready");
            Console.WriteLine("Free space: {0} MB", freeSpaceMB);
         } else {
            Console.WriteLine("Drive E: is not ready or does not exist");
         }
      } catch (Exception ex) {
         Console.WriteLine("Error accessing drive: " + ex.Message);
      }
   }
}

The output of the above code is −

Drive E: is not ready or does not exist

Conclusion

The DriveInfo class in C# provides a simple way to retrieve disk space information including free space, total size, and drive availability. Always check the IsReady property before accessing drive properties to avoid exceptions when drives are not available.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements