Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve disk information in C#?

Tags:

c#

disk

I would like to access information on the logical drives on my computer using C#. How should I accomplish this? Thanks!

like image 611
leo Avatar asked Jan 05 '09 09:01

leo


People also ask

How do I get all drives in C#?

But we can also get a collection of all computer drives in C#. We do that with the DriveInfo. GetDrives() method, which returns an array with DriveInfo objects that each represent a single computer drive (Microsoft Docs, n.d. c).

How can I get all drives?

See drives in Windows 11, 10, and 8 You can open File Explorer by pressing Windows key + E . In the left pane, select This PC, and all drives are shown on the right.


2 Answers

For most information, you can use the DriveInfo class.

using System; using System.IO;  class Info {     public static void Main() {         DriveInfo[] drives = DriveInfo.GetDrives();         foreach (DriveInfo drive in drives) {             //There are more attributes you can use.             //Check the MSDN link for a complete example.             Console.WriteLine(drive.Name);             if (drive.IsReady) Console.WriteLine(drive.TotalSize);         }     } } 
like image 130
Vinko Vrsalovic Avatar answered Sep 23 '22 21:09

Vinko Vrsalovic


If you want to get information for single/specific drive at your local machine. You can do it as follow using DriveInfo class:

//C Drive Path, this is useful when you are about to find a Drive root from a Location Path. string path = "C:\\Windows";  //Find its root directory i.e "C:\\" string rootDir = Directory.GetDirectoryRoot(path);  //Get all information of Drive i.e C DriveInfo driveInfo = new DriveInfo(rootDir); //you can pass Drive path here e.g   DriveInfo("C:\\")  long availableFreeSpace = driveInfo.AvailableFreeSpace; string driveFormat = driveInfo.DriveFormat; string name = driveInfo.Name; long totalSize = driveInfo.TotalSize; 
like image 27
mmushtaq Avatar answered Sep 25 '22 21:09

mmushtaq