简体   繁体   English

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

[英]How come the “SerialNumber” property of the “Win32_CDROMDrive” class returns null when asking for it separately from WMI?

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 . 当从这个SELECT SerialNumber FROM Win32_CDROMDrive获取WMI类Win32_CDROMDrive中的属性SerialNumber的值时,它会通过NullReferenceException除非我将查询更改为SELECT * FROM Win32_CDROMDrive Then loop arround all the properties including the SerialNumber in-which in that case is not null. 然后循环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 更新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作为循环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 更新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

Or to be specific (to the CDROMDrive) 或者具体到(CDROMDrive)

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

Or to be specific (to the SerialNumber of the CDROMDrive 或者具体到( CDROMDriveSerialNumber

SELECT SerialNumber FROM Win32_PhysicalMedia WHERE Tag Like '%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属性?

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. 我刚刚在我的PC上进行了测试,在我的情况下,似乎是因为我所拥有的唯一实例上的SerialNumber属性为null。 As it seems, WMI is no working properly when looking for a property that is NULL (that is on my case). 看起来,当找到一个NULL属性时,WMI无法正常工作(就我而言)。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Win32_CDROMDrive WMI类获取接口类型? - How to get the interface type from Win32_CDROMDrive WMI class? 使用WQL从WMI查询Win32_NTLogEvent类时,TimeGenerated属性是否基于计算机或GMT的本地时间? - When querying the Win32_NTLogEvent Class from WMI with WQL is the TimeGenerated property based on Local time of the computer or GMT? Win32_ComputerSystem WMI类的SystemType属性的值奇怪吗? - Strange values of SystemType property of Win32_ComputerSystem WMI class? 如何通过 WMI 从“Win32_ProcessStopTrace”类获取附加信息? - How to obtain additional info from the 'Win32_ProcessStopTrace' class through WMI? C#使用WMI查询Win32_Fan class和风扇转速返回null? - C# using WMI to query Win32_Fan class and fan speed return null? TimeStamp_Sys100NS从Win32_PerfFormattedData_PerfProc_Process wmi返回null - TimeStamp_Sys100NS returning null from Win32_PerfFormattedData_PerfProc_Process wmi Win32_PhysicalMedia-获取IDE序列号 - Win32_PhysicalMedia - get IDE SerialNumber C# WMI 方法 IsActivated 从 Win32_Tpm class 抛出“无效的方法参数” - C# WMI method IsActivated from Win32_Tpm class throws "Invalid method Parameter(s)" 查询WMI类Win32_ShadowCopy时初始化失败 - Initialization failure when querying WMI-class Win32_ShadowCopy 在 Win32_ScheduledJob 上运行 WMI 查询不返回任何结果 - Running WMI query on Win32_ScheduledJob returns no results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM