Trouble with escape character in WMI query

Deadly 提交于 2021-01-27 21:15:59

问题


processName.Name = @"\\dfs\ns1\Application_Data\tiny\Development\tinyLOS\tinyLOS.exe";

string wmiQuery = string.Format("select CommandLine from Win32_Process where PathName='{0}'", processName.Name);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get(); 

I am trying to run the above code in C# using WMI but i keep getting an invalid query error when it executes. I suspect it's a problem with escaping the back slash but i thought i had accounted for that with the @. Am i missing something simple because this looks like it should execute ok?

(Note: the query executes when the back slashes are removed)


回答1:


You need to pass escaped slashes and it's ExecutablePath not PathName

wmiQuery = @"SELECT CommandLine FROM Win32_Process WHERE ExecutablePath =
            'C:\\Program Files\\Microsoft Security Client\\msseces.exe'";

var searcher = new ManagementObjectSearcher("root\\CIMV2", wmiQuery);

foreach (ManagementObject queryObj in searcher.Get())
    Console.WriteLine("CommandLine: {0}", queryObj["CommandLine"]);

For

CommandLine: "C:\Program Files\Microsoft Security Client\msseces.exe" -hide -runkey



来源:https://stackoverflow.com/questions/31568343/trouble-with-escape-character-in-wmi-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!