Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to get free space in a drive
To get the free space in a drive, use the AvailableFreeSpace and TotalFreeSpace properties of the DriveInfo class in C#. This is useful for monitoring disk space and making decisions based on available storage.
Syntax
Following is the syntax for creating a DriveInfo object and accessing free space properties −
DriveInfo driveInfo = new DriveInfo("DriveLetter");
long availableSpace = driveInfo.AvailableFreeSpace;
long totalFreeSpace = driveInfo.TotalFreeSpace;
Properties
| Property | Description |
|---|---|
AvailableFreeSpace |
Gets the amount of available free space on a drive, in bytes, available to the current user. |
TotalFreeSpace |
Gets the total amount of free space available on a drive, in bytes. |
TotalSize |
Gets the total size of storage space on a drive, in bytes. |
Using DriveInfo to Check Available Space
The following example demonstrates how to get free space information for a specific drive −
using System;
using System.IO;
public class Demo {
public static void Main() {
DriveInfo dInfo = new DriveInfo("C");
// Get available free space
Console.WriteLine("Available Free Space: " + dInfo.AvailableFreeSpace + " bytes");
// Get total free space
Console.WriteLine("Total Free Space: " + dInfo.TotalFreeSpace + " bytes");
// Get total size of the drive
Console.WriteLine("Total Size: " + dInfo.TotalSize + " bytes");
// Calculate used space
long usedSpace = dInfo.TotalSize - dInfo.TotalFreeSpace;
Console.WriteLine("Used Space: " + usedSpace + " bytes");
}
}
The output of the above code is −
Available Free Space: 245760000000 bytes Total Free Space: 245760000000 bytes Total Size: 500000000000 bytes Used Space: 254240000000 bytes
Converting Bytes to Human-Readable Format
Since the values are returned in bytes, you might want to convert them to more readable units like GB or MB −
using System;
using System.IO;
public class DriveSpaceDemo {
public static void Main() {
DriveInfo dInfo = new DriveInfo("C");
// Convert bytes to GB
double availableGB = (double)dInfo.AvailableFreeSpace / (1024 * 1024 * 1024);
double totalGB = (double)dInfo.TotalSize / (1024 * 1024 * 1024);
double usedGB = totalGB - availableGB;
Console.WriteLine("Drive: " + dInfo.Name);
Console.WriteLine("Total Size: " + totalGB.ToString("F2") + " GB");
Console.WriteLine("Available Space: " + availableGB.ToString("F2") + " GB");
Console.WriteLine("Used Space: " + usedGB.ToString("F2") + " GB");
// Calculate percentage
double usedPercentage = (usedGB / totalGB) * 100;
Console.WriteLine("Used Percentage: " + usedPercentage.ToString("F1") + "%");
}
}
The output of the above code is −
Drive: C:\ Total Size: 465.66 GB Available Space: 228.91 GB Used Space: 236.75 GB Used Percentage: 50.8%
Checking All Available Drives
You can also get information for all drives on the system −
using System;
using System.IO;
public class AllDrivesDemo {
public static void Main() {
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives) {
if (drive.IsReady) {
double freeSpaceGB = (double)drive.AvailableFreeSpace / (1024 * 1024 * 1024);
double totalSpaceGB = (double)drive.TotalSize / (1024 * 1024 * 1024);
Console.WriteLine("Drive: " + drive.Name);
Console.WriteLine("Type: " + drive.DriveType);
Console.WriteLine("Free Space: " + freeSpaceGB.ToString("F2") + " GB");
Console.WriteLine("Total Space: " + totalSpaceGB.ToString("F2") + " GB");
Console.WriteLine("----------");
}
}
}
}
The output of the above code is −
Drive: C:\ Type: Fixed Free Space: 228.91 GB Total Space: 465.66 GB ---------- Drive: D:\ Type: Fixed Free Space: 150.25 GB Total Space: 200.00 GB ----------
Conclusion
The DriveInfo class provides easy access to drive space information through AvailableFreeSpace and TotalFreeSpace properties. This is essential for applications that need to monitor disk usage or ensure sufficient space before performing file operations.
