How to Get all drives in C#
By Tan Lee Published on May 06, 2024 574
In C#, you can retrieve all drives available in the system using the DriveInfo class from the System.IO namespace.
How to get all drives in C#
Here's a simple example demonstrating how to get all drives.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Get all drives in the system DriveInfo[] driveInfos = DriveInfo.GetDrives(); // Iterate through each drive and print its information foreach (DriveInfo drive in driveInfos) { Console.WriteLine($ "Drive: {drive.Name}" ); Console.WriteLine($ "Drive Type: {drive.DriveType}" ); if (drive.IsReady) { Console.WriteLine($ "Volume Label: {drive.VolumeLabel}" ); Console.WriteLine($ "File System: {drive.DriveFormat}" ); Console.WriteLine($ "Total Size: {drive.TotalSize} bytes" ); Console.WriteLine($ "Available Free Space: {drive.AvailableFreeSpace} bytes" ); } } |
This code retrieves an array of DriveInfo
objects representing all available drives in the system. Then, it iterates through each DriveInfo
object and prints information such as drive name, drive type, volume label, file system, total size, and available free space.
1 2 3 4 |
// c# get logial drivers string [] drives = Directory.GetLogicalDrives(); foreach ( string drive in drives) Console.WriteLine(drive); |
You can also use GetLogicalDrives method from the System.IO
namespace to retrieve an array driver name, then display the logical drives of the current computer in c#.
Categories
Popular Posts
11 Things You Didn't Know About Cloudflare
Dec 19, 2024
K-WD Tailwind CSS Admin Dashboard Template
Nov 17, 2024
Focus Admin Dashboard Template
Nov 18, 2024
Freedash bootstrap lite
Nov 13, 2024