简体   繁体   中英

Given a hard disk letter, how can I get the manufacturer serial number?

I'm having lot of troubles trying to determine the manufacturer serial number of a drive, and which could be the fastest way to do it (rather than calling WMI ).

The info that I can use to query the manufacturer serial number would be the drive letter (eg. C:\\ ) or the numeric serial number that is assigned by the operating system to a drive (eg. 4030771280 ), that info I retrieve it by specifying a drive letter calling GetVolumeInformation function.

There are many examples and Q/A in Stackoverflow about how to retrieve the manufacturer's serial number using WMI , however all those questions and answers are for the CURRENT drive, and the Win32_PhysicalMedia and Win32_DiskDrive classes doesn't seems to expose a property that I could use with a WHERE to query the serial number of a specific drive given a drive letter or its system's serial number

I'm open to solutions that implies intermediary steps such as " first you need to get a device id from drive letter then you can use that value to query WMI " (of course preferably using WinAPI functions to avoid the negative performance impact of WMI calls), the problem is I don't know how to do a required retrieval like that quoted, because the Volume Management Functions doesn't seems to have a usefull function to retrieve disk info from a drive letter, just the GetVolumeInformation function on which I only can retrieve the system's serial info that is a value that I cannot use to do a query on WMI, and the disk caption, which I cannot use too because more than one disk could have the same caption...

There's no simple way to get serial number of your hard drive based on Volume letter - eg in case or RAID 5 there's more than one physical drive that makes up volume. To get serial number of hard drive(s) will be easier :

// A little bit modified code from https://stackoverflow.com/questions/24022130/generate-activation-key-from-hardware-id
public string identifier(string wmiClass, string wmiProperty)
        {
            string result = "";
            System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass);
            System.Management.ManagementObjectCollection moc = mc.GetInstances();
            foreach (System.Management.ManagementObject mo in moc)
            {
                //Only get the first one
                if (result == "")
                {
                    try
                    {
                        result = mo[wmiProperty].ToString();
                        break;
                    }
                    catch
                    {
                    }
                }
            }
            return result;
        }

And use it like :

identifier("Win32_DiskDrive", "SerialNumber"));

This source code won't give you what you want, but might lead to finding solution.

Edit : without WMI there's no easy way to get hardware info - you would most likely have to write your own libraries to interact with hardware.

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