Win32 Classes in Windows OS are groups of Hardware, Software or OS classes (along with others) that contains features and attributes depending on the class selected. My problem is with the Win32_CDROMDrive that has many properties of the connected ODD device like Caption
, Description
, Drive
, Id
, Name
, etc.). The problem (specifically) is with the property SerialNumber
which returns the identifier of the drive by the manufacture. It returns a null in a case and returns an actual value in a different case! The code will walk you through:
Here is how to list all the properties of the Win32_CDROMDrive
(SerialNumber
exist with an actual value).
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
foreach (ManagementObject mo in searcher.Get())
foreach (PropertyData pd in mo.Properties)
Console.WriteLine("{0}:- {1}", pd.Name, pd.Value);
To get a single property value (like Name
) from the same class the query is changed to (and it will return the value of Name
).
var searcher = new ManagementObjectSearcher("SELECT Name FROM Win32_CDROMDrive");
To get the value of the SerialNumber
property the query is changed to (and here is the problem, it will return null while did not before!)
var searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_CDROMDrive");
More odd behavior is when I try to get the value of a property with other random one like this (it will return actual values for both Name
and SerialNumber
)
var searcher = new ManagementObjectSearcher("SELECT SerialNumber, SCSIPort FROM Win32_CDROMDrive");
While if with a property with a different initial (it will return null for the SerialNumber
and actual value for the Name
)
var searcher = new ManagementObjectSearcher("SELECT SerialNumber, Name FROM Win32_CDROMDrive");
I know I’ve asked a similar question but I can’t delete it or edit it to this one (which what I have got so far) cause it has good information that might be helpful for someone (even if it is simple).
I just want to get the SerialNumber
solely like this SELECT SerialNumber FROM Win32_CDROMDrive
without extracting the whole values and choosing between them.