我想找到名称以param1开头的结果,并以param2结尾,但我的代码不起作用
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();
怎么了?
比较
string wmiQuery = string.Format("SELECT CommandLine FROM Win32_Process WHERE Name LIKE '{0}%'", param1);
运作良好
答案 0 :(得分:14)
试试这个:
string wmiQuery = string.Format("SELECT CommandLine FROM Win32_Process WHERE Name LIKE '{0}%{1}'", param1, param2);
添加一些测试信息:
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"] );
}
输出:
查询:SELECT Name,ProcessID FROM Win32_Process WHERE名称LIKE 'WPF%的.exe'
[896]名称:WPFFontCache_v0400.exe
答案 1 :(得分:8)
要构建WMI查询,请使用Microsoft提供的免费工具WMI Code Creator,使用不同的编程语言VBS,VB.NET,C#
下载