如何构造WMI查询

本文关键字:查询 WMI 何构造 | 更新日期: 2023-09-27 17:58:46

我想找到Name以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);

运行良好

如何构造WMI查询

试试这个:

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名称,ProcessID FROMWin32_Process WHERE Name LIKE'wpf%.exe'
[896]名称:WPFFontCache_v0400.exe

要构建WMI查询,请使用Microsoft的免费工具WMI Code Creator,使用不同的编程语言VBS、VB.NET、C#

从下载http://www.microsoft.com/downloads/en/details.aspx?familyid=2cc30a64-ea15-4661-8da4-55bbc145c30e&displaylang=en