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
How to get complete drive information using C#?
Drive information of an Operating System includes key details about storage drives such as drive name, volume label, free space, total size, drive format, and drive type. In C#, you can retrieve this information using the DriveInfo class from the System.IO namespace.
Syntax
Following is the syntax for creating a DriveInfo object and accessing its properties −
DriveInfo driveInfo = new DriveInfo("DriveLetter");
string name = driveInfo.Name;
long freeSpace = driveInfo.AvailableFreeSpace;
DriveType type = driveInfo.DriveType;
DriveInfo Properties
| Property | Description |
|---|---|
| Name | Gets the drive name (e.g., "C:") |
| VolumeLabel | Gets or sets the volume label of the drive |
| AvailableFreeSpace | Gets available free space for current user in bytes |
| TotalFreeSpace | Gets total free space available on the drive in bytes |
| TotalSize | Gets total size of the drive in bytes |
| DriveFormat | Gets file system format (e.g., "NTFS", "FAT32") |
| DriveType | Gets drive type (Fixed, Removable, Network, etc.) |
Getting Information for a Specific Drive
Example
using System;
using System.IO;
class Program {
static void Main() {
DriveInfo driveInfo = new DriveInfo("C");
Console.WriteLine("Drive Name: " + driveInfo.Name);
Console.WriteLine("Volume Label: " + driveInfo.VolumeLabel);
Console.WriteLine("Available Free Space: " + driveInfo.AvailableFreeSpace + " bytes");
Console.WriteLine("Total Free Space: " + driveInfo.TotalFreeSpace + " bytes");
Console.WriteLine("Total Size: " + driveInfo.TotalSize + " bytes");
Console.WriteLine("Drive Format: " + driveInfo.DriveFormat);
Console.WriteLine("Drive Type: " + driveInfo.DriveType);
}
}
The output of the above code is −
Drive Name: C:\ Volume Label: Windows Available Free Space: 156728492032 bytes Total Free Space: 156728492032 bytes Total Size: 255094845440 bytes Drive Format: NTFS Drive Type: Fixed
Getting Information for All Available Drives
Example
using System;
using System.IO;
class Program {
static void Main() {
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives) {
Console.WriteLine("--- Drive Information ---");
Console.WriteLine("Name: " + drive.Name);
Console.WriteLine("Type: " + drive.DriveType);
if (drive.IsReady) {
Console.WriteLine("Volume Label: " + drive.VolumeLabel);
Console.WriteLine("Format: " + drive.DriveFormat);
Console.WriteLine("Total Size: " + (drive.TotalSize / (1024 * 1024 * 1024)) + " GB");
Console.WriteLine("Free Space: " + (drive.AvailableFreeSpace / (1024 * 1024 * 1024)) + " GB");
} else {
Console.WriteLine("Drive is not ready");
}
Console.WriteLine();
}
}
}
The output of the above code is −
--- Drive Information --- Name: C:\ Type: Fixed Volume Label: Windows Format: NTFS Total Size: 237 GB Free Space: 146 GB --- Drive Information --- Name: D:\ Type: Fixed Volume Label: Data Format: NTFS Total Size: 931 GB Free Space: 500 GB --- Drive Information --- Name: E:\ Type: Removable Drive is not ready
Converting Bytes to Human-Readable Format
Example
using System;
using System.IO;
class Program {
static void Main() {
DriveInfo drive = new DriveInfo("C");
if (drive.IsReady) {
Console.WriteLine("Drive: " + drive.Name);
Console.WriteLine("Total Size: " + FormatBytes(drive.TotalSize));
Console.WriteLine("Used Space: " + FormatBytes(drive.TotalSize - drive.TotalFreeSpace));
Console.WriteLine("Free Space: " + FormatBytes(drive.AvailableFreeSpace));
double usagePercent = ((double)(drive.TotalSize - drive.TotalFreeSpace) / drive.TotalSize) * 100;
Console.WriteLine("Usage: " + usagePercent.ToString("F1") + "%");
}
}
static string FormatBytes(long bytes) {
string[] suffixes = { "B", "KB", "MB", "GB", "TB" };
int counter = 0;
double number = bytes;
while (Math.Round(number / 1024) >= 1) {
number = number / 1024;
counter++;
}
return string.Format("{0:n1}{1}", number, suffixes[counter]);
}
}
The output of the above code is −
Drive: C:\ Total Size: 237.5 GB Used Space: 91.4 GB Free Space: 146.1 GB Usage: 38.5%
Conclusion
The DriveInfo class in C# provides comprehensive information about system drives including space usage, format, and type. Use DriveInfo.GetDrives() to enumerate all drives or create specific DriveInfo instances for targeted drive information. Always check the IsReady property before accessing drive properties to avoid exceptions.
