为什么“Win32_CDROMDrive”类的“SerialNumber”属性在与WMI分开请求时返回null?

时间:2021-01-20 03:21:59

When acquiring the value of the property SerialNumber from the WMI class Win32_CDROMDrive like this SELECT SerialNumber FROM Win32_CDROMDrive it throughs a NullReferenceException unless i change the query to SELECT * FROM Win32_CDROMDrive. Then loop arround all the properties including the SerialNumber in-which in that case is not null.

当从这个SELECT SerialNumber FROM Win32_CDROMDrive获取WMI类Win32_CDROMDrive中的属性SerialNumber的值时,它会通过NullReferenceException,除非我将查询更改为SELECT * FROM Win32_CDROMDrive。然后循环arround所有属性,包括SerialNumber in - 在这种情况下不是null。

And since the first method is faster than the second (not quite sure) I prefer to use it. So what is happening? Am I missing something? Note that it works perfectly fine with other properties and classes!

由于第一种方法比第二种方法更快(不太确定),我更喜欢使用它。那么发生了什么?我错过了什么吗?请注意,它与其他属性和类完美匹配!

This is my code

这是我的代码

string result = "";
var searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_CDROMDrive");
ManagementObjectCollection collec = searcher.Get();
foreach (ManagementObject obj in collec)
{
    result = obj["SerialNumber"].ToString();
    break;
}
MessageBox.Show(result);

It won't work unless i change to:

除非我改为:

var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");

Update

The first method works with the other properties of the same class and the value can be extracted without an exception. It seems like the problem is with the SerialNumber property only!

第一种方法适用于同一个类的其他属性,并且可以无异常地提取值。似乎问题仅在于SerialNumber属性!

Update 2

It seems like the problem is indeed with just SerialNumber as looping arround all the non-nulled-values of properties of the Win32_CDROMDrive will list the SerialNumber with a real value as the code below explains:

似乎问题确实只是SerialNumber作为循环arround Win32_CDROMDrive属性的所有非空值将列出具有实际值的SerialNumber,如下面的代码所示:

listView1.Items.Clear();
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_CDROMDrive");
foreach (ManagementObject mo in searcher.Get())
{
    foreach (PropertyData pd in mo.Properties)
    {
        if (pd.Value != null)
            listView1.Items.Add(pd.Name).SubItems.Add(pd.Value.ToString());
    }
}

However, if the query is changed to the specific wanted property method it will give the same error!

但是,如果查询更改为特定的所需属性方法,它将给出相同的错误!

Update 3

I managed to get the value of this naughty property without looping arround all the remaining ones via a different class Win32_PhysicalMedia which contains less properties for all connected drives (HDD, ODD, Floppy, ...) including the SerialNumber property using this WQL query

我设法获得这个顽皮属性的价值,而没有通过另一个类Win32_PhysicalMedia循环所有其余的属性Win32_PhysicalMedia,其包含所有连接的驱动器(HDD,ODD,软盘,...)的较少属性,包括使用此WQL查询的SerialNumber属性

SELECT * FROM Win32_PhysicalMedia

SELECT * FROM Win32_PhysicalMedia

Or to be specific (to the CDROMDrive)

或者具体到(CDROMDrive)

SELECT * FROM Win32_PhysicalMedia WHERE Tag Like '%CD%'

SELECT * FROM Win32_PhysicalMedia WHERE标签类似'%CD%'

Or to be specific (to the SerialNumber of the CDROMDrive

或者具体到(CDROMDrive的SerialNumber

SELECT SerialNumber FROM Win32_PhysicalMedia WHERE Tag Like '%CD%'

SELECT SerialNumber FROM Win32_PhysicalMedia WHERE标签类似'%CD%'

var searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_PhysicalMedia WHERE TAG LIKE '%CD%'");
ManagementObjectCollection collec = searcher.Get();
foreach (ManagementObject obj in collec)
{
     Console.WriteLine(obj["SerialNumber"].ToString());
}
Console.Read();

But i can't consider this to be an answer as my question is why WQL doesn't allow specifing a record inside the SELECT statement to the (and only the) SerialNumber property of the CDROMDrive class?

但我不能认为这是一个答案,因为我的问题是为什么WQL不允许在SELECT语句中指定CDROMDrive类的(并且只有)SerialNumber属性?

1 个解决方案

#1


0  

I just tested on my PC and on my case it seems to be because SerialNumber property is null on the only instance I have. As it seems, WMI is no working properly when looking for a property that is NULL (that is on my case).

我刚刚在我的PC上进行了测试,在我的情况下,似乎是因为SerialNumber属性在我唯一的实例上是null。看起来,WMI在查找NULL属性时就无法正常工作(就我而言)。

Anyway, you can use ORMi to work with WMI and do all the work using Linq.

无论如何,您可以使用ORMi来使用WMI并使用Linq完成所有工作。

Example:

WMIHelper helper = new WMIHelper("root\\CimV2");

var data = helper.Query("SELECT * FROM Win32_CDROMDrive").Where(p => p.SerialNum == "yourSerialNum");

#1


0  

I just tested on my PC and on my case it seems to be because SerialNumber property is null on the only instance I have. As it seems, WMI is no working properly when looking for a property that is NULL (that is on my case).

我刚刚在我的PC上进行了测试,在我的情况下,似乎是因为SerialNumber属性在我唯一的实例上是null。看起来,WMI在查找NULL属性时就无法正常工作(就我而言)。

Anyway, you can use ORMi to work with WMI and do all the work using Linq.

无论如何,您可以使用ORMi来使用WMI并使用Linq完成所有工作。

Example:

WMIHelper helper = new WMIHelper("root\\CimV2");

var data = helper.Query("SELECT * FROM Win32_CDROMDrive").Where(p => p.SerialNum == "yourSerialNum");