Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lookup Hard Drive model with C#?

Tags:

c#

In part of a program I am writing, I'm trying to pull device information about specified local hard drives. I've been able to create a few value returning methods using the DriveInfo class like this:

//Gets drive format
    public string GetDriveFormat(string driveName)
    {
        foreach (DriveInfo drive in DriveInfo.GetDrives())
        {
            if (drive.IsReady && drive.Name == driveName)
            {
                return drive.DriveFormat;
            }
        }
        return "";
    }
//Example of use
   MessageBox.Show(GetDriveFormat("C:\\"));

The problem I'm running into now is that there doesn't seem to be a Model property to the DriveInfo class. I've looked all over but am unable to find a way to construct a value returning method that will return the model of a drive like what is viewable in device manager.

Any help would be greatly appreciated, Thanks!

like image 751
user1959800 Avatar asked Oct 21 '22 20:10

user1959800


1 Answers

Unfortunately, you cannot get the Drive's Manufacturer and Model using the DriveInfo class.

You'll have to resort back to WMI:

WqlObjectQuery q = new WqlObjectQuery("SELECT * FROM Win32_DiskDrive");
using (ManagementObjectSearcher res = new ManagementObjectSearcher(q)) {
foreach (ManagementObject o in res.Get()) {
Console.WriteLine("Caption = " + o["Caption"]);
Console.WriteLine("DeviceID = " + o["DeviceID"]);
Console.WriteLine("Decsription = " + o["Description"]);
Console.WriteLine("Manufacturer = " + o["Manufacturer"]);
Console.WriteLine("MediaType = " + o["MediaType"]);
Console.WriteLine("Model = " + o["Model"]);
Console.WriteLine("Name = " + o["Name"]);
// only in Vista, 2008 & etc: //Console.WriteLine("SerialNumber = " + o["SerialNumber"]);
} }

Not sure if you need to consider mounted drives as well:

foreach(ManagementObject volume in new ManagementObjectSearcher("Select * from Win32_Volume" ).Get())
{
 ...
}
like image 116
Jeremy Thompson Avatar answered Nov 01 '22 13:11

Jeremy Thompson