ElektroStudios
ElektroStudios

Reputation: 20494

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...

Upvotes: 0

Views: 1286

Answers (1)

Vytautas Plečkaitis
Vytautas Plečkaitis

Reputation: 861

There's no simple way to get serial number of your hard drive based on Volume letter - e.g. 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.

Upvotes: 1

Related Questions