Click here to Skip to main content
15,892,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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!
I just want to get the SerialNumber solely like this SELECT SerialNumber FROM Win32_CDROMDrive without extracting the whole values and choosing between them.

What I have tried:

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).
C#
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)
C#
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)
C#
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)
C#
var searcher = new ManagementObjectSearcher("SELECT SerialNumber, Name FROM Win32_CDROMDrive");
Again can the SerialNumber be extracted solely like this SELECT SerialNumber FROM Win32_CDROMDrive without extracting the whole values and choosing between them.
Posted
Updated 6-Jul-18 10:16am

1 solution

You're making the mistake of assuming that information is actually there.

The problem is WMI makes no requirements of manufacturer to provide the data in every property in every WMI class.

If the serial number property returns null, it's just not provided by the manufacturer of the device to WMI.

Your WMI query is correct. There's just no data for it to return.
 
Share this answer
 
Comments
Member 13901685 6-Jul-18 16:26pm    
It is already there as i mentioned in the first block of code, and can be extracted with an actual value. Except i want to aquire it separately without getting the values of the remaining properties, then loop arround each one of them to get what I want.
Dave Kreskowiak 6-Jul-18 16:49pm    
The mistake is that you're expecting one ManagementObject to come back. You're actually getting a collection of them, in a ManagementObjectCollection:
foreach (ManagementObjectCollection collection in searcher.Get())
{
    foreach (ManagementObject obj in collection)
    {
        foreach (PropertyData pd in mo.Properties)
        {
            Console.WriteLine("{0}:- {1}", pd.Name, pd.Value == null ? "null" : p.Value.ToString());
        }
    }
}
Member 13901685 6-Jul-18 16:57pm    
You just replaced the null value with a null string. Try your code with those two queries
'SELECT * FROM Win32_CDROMDrive' (SerialNumber isn't null)
'SELECT SerialNumber FROM Win32_CDROMDrive' (SerialNumber is null)
And the Collection can have one single object if the query2 is used!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900