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
Get all drives in C#
In C#, you can retrieve information about all drives on a system using the DriveInfo.GetDrives() method. This method returns an array of DriveInfo objects representing all logical drives on the current system, including hard drives, network drives, CD-ROMs, and other storage devices.
Syntax
Following is the syntax for getting all drives −
DriveInfo[] drives = DriveInfo.GetDrives();
To iterate through the drives and access their properties −
foreach (DriveInfo drive in drives) {
Console.WriteLine(drive.Name);
}
Using GetDrives() to Display Drive Names
The simplest approach is to get all drive names and display them −
using System;
using System.IO;
public class Demo {
public static void Main() {
var drives = DriveInfo.GetDrives();
Console.WriteLine("Available drives:");
foreach (DriveInfo drive in drives) {
Console.WriteLine(drive.Name);
}
}
}
The output of the above code on Windows is −
Available drives: C:\ D:\ E:\
Using GetDrives() with Detailed Drive Information
You can access additional properties of each drive such as drive type, available space, and total size −
using System;
using System.IO;
public class DriveDetails {
public static void Main() {
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives) {
Console.WriteLine("Drive: " + drive.Name);
Console.WriteLine("Type: " + drive.DriveType);
if (drive.IsReady) {
Console.WriteLine("Total Size: " + (drive.TotalSize / (1024 * 1024 * 1024)) + " GB");
Console.WriteLine("Available Space: " + (drive.AvailableFreeSpace / (1024 * 1024 * 1024)) + " GB");
}
Console.WriteLine();
}
}
}
The output of the above code is −
Drive: C:\ Type: Fixed Total Size: 465 GB Available Space: 298 GB Drive: D:\ Type: CDRom
Using GetDrives() to Filter Ready Drives Only
Some drives may not be ready (like empty CD drives). You can filter to show only ready drives −
using System;
using System.IO;
using System.Linq;
public class ReadyDrives {
public static void Main() {
var readyDrives = DriveInfo.GetDrives().Where(d => d.IsReady);
Console.WriteLine("Ready drives:");
foreach (DriveInfo drive in readyDrives) {
Console.WriteLine($"{drive.Name} - {drive.DriveFormat} ({drive.DriveType})");
}
}
}
The output of the above code is −
Ready drives: C:\ - NTFS (Fixed)
DriveInfo Properties
| Property | Description |
|---|---|
Name |
Gets the name of the drive (e.g., "C:") |
DriveType |
Gets the drive type (Fixed, Removable, Network, etc.) |
IsReady |
Indicates whether the drive is ready for read/write operations |
TotalSize |
Gets the total size of the drive in bytes |
AvailableFreeSpace |
Gets the available free space in bytes |
DriveFormat |
Gets the file system format (NTFS, FAT32, etc.) |
Conclusion
The DriveInfo.GetDrives() method provides an easy way to enumerate all drives on a system and access their properties. Always check the IsReady property before accessing size-related properties to avoid exceptions on drives that are not ready for operations.
