I would like to access information on the logical drives on my computer using C#. How should I accomplish this? Thanks!
+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
2009-01-05 09:27:49
good job,thanks!
leo
2009-01-05 09:37:14
What about drive info on a machine other than the local machine?
flipdoubt
2009-01-05 12:13:40
For network mounted drives this works, reports drive type as "Network". For remote querying, I think you should ask a different question.
Vinko Vrsalovic
2009-01-05 13:19:12
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:
Sean James
2009-01-05 09:28:07
A:
Check the DriveInfo Class and see if it contains all the info that you need.
bruno conde
2009-01-05 09:28:19
+2
A:
Use System.IO.DriveInfo class http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx
rravuri
2009-01-05 09:29:28
+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
2009-09-06 05:43:01
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
2009-09-06 06:09:46
+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
2010-10-20 13:39:28