سؤال

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