2013-12-16 117 views
0

我試圖反轉以下查詢:我的WMI查詢有什麼問題?

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

分爲:

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

好吧,我只是想通了。我必須將查詢中路徑中的斜槓和轉義字符加倍。

C#編譯器和WMI SQL實現都需要轉義斜線我猜。愚蠢的電腦。

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