如何从win32 WMI类中选择单个字段? [英] How to SELECT a single field from a win32 WMI class?

查看:83
本文介绍了如何从win32 WMI类中选择单个字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows操作系统中的 Win32 Classes 硬件,软件或操作系统(以及其他)包含根据所选类别的功能和属性。我的问题是 Win32_CDROMDrive ,它具有连接的ODD设备的许多属性,如标题描述驱动器 Id 名称等)。问题(具体)是属性 SerialNumber ,它返回制造商的驱动器标识符。 的情况下返回null 并在不同的情况下返回实际值!

我只想获得 SerialNumber 完全像 SELECT SerialNumber FROM Win32_CDROMDrive ,无需提取整个值并在它们之间进行选择。



我尝试过:



以下是如何列出 Win32_CDROMDrive的所有属性 SerialNumber 存在实际值)。

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

要从同一个类获取单个属性值(如 Name ),查询将更改为(并且它将返回值名称)。

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

要获取 SerialNumber 属性的值,查询将更改为(这是问题所在, it < b> 将返回null )

To get the value of the SerialNumber property the query is changed to (and here is the problem, it will return null)

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

更奇怪的行为是wh我尝试使用其他随机的属性获取属性的值(它将返回实际值)

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)

var searcher = new ManagementObjectSearcher("SELECT SerialNumber, SCSIPort FROM Win32_CDROMDrive");

如果使用具有不同初始值的属性(它将为 SerialNumber 返回null,并为名称)

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

再次可以 SerialNumber 可以像这样 SELECT SerialNumber 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.

推荐答案

你假设信息确实在那里是错误的。



问题是WMI没有要求制造商在每个WMI类的每个属性中提供数据。 br />


如果序列号属性返回null,则设备制造商不会向WMI提供。



您的WMI查询是否正确。它没有数据可以返回。
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.


这篇关于如何从win32 WMI类中选择单个字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆