我的WMI查询出了什么问题?

时间:2013-12-16 12:21:07

标签: c# wmi wmi-query

我正在尝试反转以下查询:

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = 4856")) {
    foreach (ManagementObject mo in searcher.Get()) {
        Debug.WriteLine(mo["CommandLine"]);
    }
}

返回预期结果:

  

C:\的Windows \ EXPLORER.EXE

INTO:

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT ProcessId FROM Win32_Process WHERE CommandLine = 'C:\\Windows\\Explorer.EXE'")) {
    foreach (ManagementObject mo in searcher.Get()) {
        Debug.WriteLine(mo["ProcessId"]);
    }
}

会引发Invalid query异常,而不是进程ID。

1 个答案:

答案 0 :(得分:1)

好的,我刚想通了。我必须在查询的路径中加倍斜杠和转义字符。

我认为C#编译器和WMI SQL实现都需要转义斜杠。愚蠢的电脑。

SELECT ProcessId FROM Win32_Process WHERE CommandLine = 'C:\\\\windows\\\\explorer.EXE'