How to identify system hard drive using Win32_DiskDrive
Solution 1
The documentation you linked has the answer:
string SerialNumber;
uint32 Signature;
Your management object will have those properties in it.
If you're getting null for those values then the problem is that you're using a version of Windows that doesn't support them.
SerialNumber
...
Windows Server 2003 and Windows XP: This property is not available.
In which case you have to use the Signature property, but this requires XP SP3 I believe.
Solution 2
you can get it as :
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject queryObj in searcher.Get())
{
Console.WriteLine("SerialNumber: {0}", queryObj["SerialNumber"]);
Console.WriteLine("Signature: {0}", queryObj["Signature"]);
}
}
catch (ManagementException e)
{
}
}
Related videos on Youtube
Dragan Radivojevic
Updated on October 30, 2022Comments
-
Dragan Radivojevic over 1 year
I'm using WMI to get info about hard drives on a computer but I just can't find the property that will allow me to identify which hard drive is used as system drive where Windows is installed.
ManagementObjectSearcher mos_HDD = new ManagementObjectSearcher("select * from Win32_DiskDrive");
I tried iterating through all properties but neither one looks like it holds the info I need.
foreach (ManagementObject mo_HDD in mos_HDD.Get()) { Console.WriteLine("HDD Properties:"); foreach (PropertyData pd in mo_HDD.Properties) { Console.WriteLine("\tName: {0} \tValue: {1}", pd.Name, pd.Value != null ? pd.Value.ToString() : "NULL"); } }
I've also looked at MSDN documentation but w/o luck.
What I'm trying to do here is to get some kind of identifier for a system drive (such as Signature or Serial number).
Any help to get this info is highly appreciated.
-
Dragan Radivojevic almost 11 yearsThanks for the details. I checked both of these and SerialNumber looks just fine but the problem is when there are several hard disks in the system. How will I know which one holds the system partition?
-
PhonicUK almost 11 yearsFind the drive containing a
Win32_LogicalDisk
where theDeviceID
==C:
(or whatever drive letter Windows is installed on) -
Dragan Radivojevic over 10 yearsI couldnt find the solution to this but this is the closest as it gets so I'm marking it as answer.