简单查询在PowerShell中工作正常:
gwmi -Query "select * from Win32_DependentService where Antecedent='\\\\MYPC\\root\\cimv2:Win32_SystemDriver.Name=`"WdFilter`"'"
但它在C#中不起作用:
var scope = new ManagementScope("\\\\MYPC\\Root\\Cimv2");
scope.Connect();
var searcher = new ManagementObjectSearcher(scope, new ObjectQuery("select * from Win32_DependentService where Antecedent = '\\\\MYPC\\root\\cimv2:Win32_SystemDriver.Name=\"WdFilter\"'"));
ManagementObjectCollection queryCollection = searcher.Get();
var count = queryCollection.Count; // "Invalid query" exception here!!!
C#查询有什么问题? 谢谢!
答案 0 :(得分:0)
固定!我们需要更多的反斜杠(最多8个!:)。正确的代码是:
var scope = new ManagementScope("\\\\MYPC\\Root\\Cimv2");
var searcher = new ManagementObjectSearcher(scope, new ObjectQuery("select * from Win32_DependentService where Antecedent='\\\\\\\\MYPC\\\\root\\\\cimv2:Win32_SystemDriver.Name=\"WdFilter\"'"));
var queryCollection = searcher.Get();
var count = queryCollection.Count; // it's OK now
希望对其他人有帮助:))