using c# how can I extract information about the hard drives present on the local machine

11,100

Solution 1

You can use WMI Calls to access info about the hard disks.

//Requires using System.Management; & System.Management.dll Reference

ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); 
disk.Get(); 
Console.WriteLine("Logical Disk Size = " + disk["Size"] + " bytes"); 
Console.WriteLine("Logical Disk FreeSpace = " + disk["FreeSpace"] + "bytes");

Solution 2

You should use the System.Management namespace:

System.Management.ManagementObjectSearcher ms =
    new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject mo in ms.Get())
{
    System.Console.Write(mo["Model");
}

For details on the members of the Win32_DiskDrive class, check out:

http://msdn.microsoft.com/en-us/library/aa394132(VS.85).aspx

Solution 3

The easiest way is to use WMI to get the required information. Take at look at the documentation for Win32___DiskDrive in MSDN, which contains a variety of standard drive properties. You can also try using the MSStorageDriver_ATAPISmartData WMI class, which I can't find any docs for at the moment, but should have all of the SMART data that you're looking for. Here's some basic sample code to enumerate all drives and get their properties:

ManagementClass driveClass = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection drives = driveClass.GetInstances();
foreach (ManagementObject drive in drives) 
{ 
    foreach (PropertyData property in drive.Properties)
    {
        Console.WriteLine("Property: {0}, Value: {1}", property.Name, property.Value);        
    }
    Console.WriteLine();
}
Share:
11,100

Related videos on Youtube

SND
Author by

SND

CEO of software start-up - Inductive.co.nz Our first product is ChessChecker.com, which helps chess players convert their over-the-board chess games to electronic games stored in the cloud. I'm based in Christchurch, NZ. I enjoy listening to people, and working with them to solve problems. My development is most done using the .net stack. I'm an ideas person and enjoy Chess, Poker and Mountain Biking.

Updated on April 15, 2022

Comments

  • SND
    SND about 2 years

    I'm looking to get data such as Size/Capacity, Serial No, Model No, Heads Sectors, Manufacturer and possibly SMART data.

  • Lyuben Todorov
    Lyuben Todorov about 12 years
    any idea if you can check read/write speeds with a test file using WMI ?