2
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)

mbrem
  • 91
  • 1
  • 9

1 Answers1

2

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

Alex K.
  • 171,639
  • 30
  • 264
  • 288