mbrem
mbrem

Reputation: 91

Trouble with escape character in WMI query

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)

Upvotes: 2

Views: 946

Answers (1)

Alex K.
Alex K.

Reputation: 175916

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

Upvotes: 2

Related Questions