How do I retrieve disk information in C#?
Asked Answered
M

6

51

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

Mucus answered 5/1, 2009 at 9:23 Comment(0)
U
78

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);
        }
    }
}
Unseasonable answered 5/1, 2009 at 9:27 Comment(2)
What about drive info on a machine other than the local machine?Bestiary
For network mounted drives this works, reports drive type as "Network". For remote querying, I think you should ask a different question.Unseasonable
H
9

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;
Handshaker answered 29/9, 2016 at 6:20 Comment(0)
J
6

What about mounted volumes, where you have no drive letter?

foreach( ManagementObject volume in 
             new ManagementObjectSearcher("Select * from Win32_Volume" ).Get())
{
  if( volume["FreeSpace"] != null )
  {
    Console.WriteLine("{0} = {1} out of {2}",
                  volume["Name"],
                  ulong.Parse(volume["FreeSpace"].ToString()).ToString("#,##0"),
                  ulong.Parse(volume["Capacity"].ToString()).ToString("#,##0"));
  }
}
Jalbert answered 6/9, 2009 at 5:43 Comment(1)
Just found it myself: ''foreach( ManagementObject volume in new ManagementObjectSearcher( "Select * from Win32_Volume" ).Get() ) { if( volume["FreeSpace"] != null ) { Console.WriteLine( "{0} = {1} out of {2}", volume["Name"], ulong.Parse( volume["FreeSpace"].ToString() ).ToString( "#,##0" ), ulong.Parse( volume["Capacity"].ToString() ).ToString( "#,##0" ) ); } } }Jalbert
E
5

Use System.IO.DriveInfo class http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx

Eglantine answered 5/1, 2009 at 9:29 Comment(0)
E
3

Check the DriveInfo Class and see if it contains all the info that you need.

Emanative answered 5/1, 2009 at 9:28 Comment(0)
C
2

In ASP .NET Core 3.1, if you want to get code that works both on windows and on linux, you can get your drives as follows:

var drives = DriveInfo
    .GetDrives()
    .Where(d => d.DriveType == DriveType.Fixed)
    .Where(d => d.IsReady)
    .ToArray();

If you don't apply both wheres, you are going to get many drives if you run the code in linux (e.g. "/dev", "/sys", "/etc/hosts", etc.).

This is specially useful when developing an app to work in a Linux Docker container.

Castra answered 4/5, 2020 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.