tags:

views:

3568

answers:

7

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

A: 

You may use classes in System.IO namespace.

For details read this and this article.

To know more about the System.IO namespace, read Microsoft's documentation here.

Frederick
+15  A: 

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);
        }
    }
}
Vinko Vrsalovic
good job,thanks!
leo
What about drive info on a machine other than the local machine?
flipdoubt
For network mounted drives this works, reports drive type as "Network". For remote querying, I think you should ask a different question.
Vinko Vrsalovic
A: 

If by disk information, you mean information about the hard drives, etc:

http://adventures-in-csharp.blogspot.com/2006/09/getting-disk-information-in-c.html

If you mean loading files from the disk:

http://www.c-sharpcorner.com/UploadFile/psingh/ReadafileusingCSharp12032005000755AM/ReadafileusingCSharp.aspx

Sean James
A: 

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

bruno conde
+2  A: 

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

rravuri
+1  A: 

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"));
  }
}
Foozinator
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" ) ); } } }
Foozinator
Please edit your answer and repost the code fragment.
SDX2000
+1  A: 

Note: The answer proposed by TheVillageIdot works very well if the operating system is windows Server 2003 or newer. Win_32 Volume does not exist on windows XP. If you run this in Windows XP an "Invalid Class" error is thrown.

DarwinIcesurfer