简体   繁体   中英

How to lookup Hard Drive model with 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!

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");
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())
{
 ...
}

I'm not entirely sure if you can get that info without using lower level api's. This post should help you achieve your goal.

http://www.codeproject.com/Articles/17973/How-To-Get-Hardware-Information-CPU-ID-MainBoard-I

Quick summary of the link:

  • Add a reference to the System.Management library

Then you can use:

var disks = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject disk in disks.Get())
{
   Console.WriteLine(disk["Model"].ToString());
   Console.WriteLine("\tSerial: " + disk["SerialNumber"]);
}

You'll need to use a lower-level API to get this information, and even then it still might not be accurate.* The internal details of the hard drives is exposed in the Win32 APIs, which you can still access in C# through WMI.

*: Note that this is still limited to the hardware information as Windows is able to see it. In some conditions, it won't or can't be accurate (eg with a RAID array, where Windows sees N drives as a single drive).

here is something that can also work for you feel free to tweak it as you please

String drive = "c";
ManagementObject disk = new ManagementObject("Win32_LogicalDisk.DeviceID=\"" + drive + ":\"");
disk.Get();
Console.WriteLine(disk["VolumeName"]);
foreach (var props in disk.Properties)
{
    Console.WriteLine(props.Name + " " + props.Value);
}
Console.ReadLine();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM