I'd like to find results that Name starts with param1, and ends with param2 but my code doesn't work
string wmiQuery = string.Format("SELECT CommandLine FROM Win32_Process WHERE Name LIKE '{0}%' AND Name LIKE '%{1}'", param1, param2);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery);
ManagementObjectCollection retObjectCollection = searcher.Get();
What's wrong?
For comparision
string wmiQuery = string.Format("SELECT CommandLine FROM Win32_Process WHERE Name LIKE '{0}%'", param1);
works well
Try this:
string wmiQuery = string.Format("SELECT CommandLine FROM Win32_Process WHERE Name LIKE '{0}%{1}'", param1, param2);
Adding some test info:
string wmiQuery = string.Format ( "SELECT Name, ProcessID FROM Win32_Process WHERE Name LIKE '{0}%{1}'", "wpf", ".exe" );
Console.WriteLine ( "Query: {0}", wmiQuery );
ManagementObjectSearcher searcher = new ManagementObjectSearcher ( wmiQuery );
ManagementObjectCollection retObjectCollection = searcher.Get ( );
foreach (ManagementObject retObject in retObjectCollection)
{
Console.WriteLine ( "[{0}]\tName: {1}", retObject[ "ProcessID" ], retObject["Name"] );
}
Output:
Query: SELECT Name, ProcessID FROM Win32_Process WHERE Name LIKE 'wpf%.exe'
[896] Name: WPFFontCache_v0400.exe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With