문제

I'm trying to invert the following query:

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

Which returns the expected result:

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

Which throws an Invalid query exception and not the process id.

도움이 되었습니까?

해결책

Ok, I just figured it out. I have to double up the slashes and escape characters in the path in the query.

Both the C# compiler and the WMI SQL implementation wants escaped slashes i guess. Stupid computers.

SELECT ProcessId FROM Win32_Process WHERE CommandLine = 'C:\\\\windows\\\\explorer.EXE'
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top