C#.NET ManagementObjectSearcher查询

时间:2010-10-04 20:07:20

标签: c# wmi wmi-query

My C#.NET 2.0应用程序使用ManagementObjectSearcher类执行两个查询:

_searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSWmi_PnPInstanceNames");

_searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSSerial_PortName");

我想将它们组合在一起,以便_searcher包含两个查询的所有结果。但是,当我尝试这样做时......

_searcher = new ManagementObjectSearcher("root\\WMI", "SELECT * FROM MSWmi_PnPInstanceNames AND MSSerial_PortName");

...抛出“无效查询”异常。有没有人对如何使这项工作有任何想法?感谢。

2 个答案:

答案 0 :(得分:1)

很遗憾,WMI查询语言不支持joinunion操作,因此您必须单独运行这些查询(因为从不同的对象库中选择)。

WMI查询语言(WQL)是ANSI SQL的一个子集 - 具有一些语义更改。并非在WQL中可以在SQL中执行的所有操作。您可以在MSDN上在线查看WMI supported query constructs

答案 1 :(得分:0)

我可以使用多个表运行查询,如下所示

 ManagementObjectSearcher searcher; 
 query = new ObjectQuery(string.Format("Select SMS_CollectToSubCollect.subCollectionID,SMS_Collection.Name " +
                                        " from SMS_CollectToSubCollect, SMS_Collection " +
                                        " where  subCollectionID in " +
                                        " (select CollectionID  from SMS_Collection where name='{0}') " +
                                        " and SMS_Collection.CollectionID  =  SMS_CollectToSubCollect.parentCollectionID " , name));
foreach (ManagementObject queryObj in searcher.Get())
{
//TODO:read values from results 
 }

希望这有帮助