1

The simple query works fine in PowerShell:

gwmi -Query "select * from Win32_DependentService where Antecedent='\\\\MYPC\\root\\cimv2:Win32_SystemDriver.Name=`"WdFilter`"'"

but it doesn't work in 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!!!

What is wrong with C# query? Thanks!

4

1 回答 1

0

固定的!我们需要更多的反斜杠(最多八个!:)。正确的代码是:

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

希望它对其他人有帮助:)

于 2015-04-13T19:12:10.140 回答