[英]WMI: Querying a specific instance of a class
I want to make changes to the config of Microsoft Windows UWF Filter ( uwfmgr.exe
) via WMI in C#.我想通过 C# 中的 WMI 更改 Microsoft Windows UWF 过滤器 (
uwfmgr.exe
) 的配置。 Now certain changes can only be done to a specific instance of the WMI class due to their nature.现在,由于其性质,某些更改只能对 WMI 类的特定实例进行。 For example:
例如:
var scope = new ManagementScope(@"root\standardcimv2\embedded");
using (var uwfClass = new ManagementClass(scope.Path.Path, "UWF_Servicing", null))
{
var instances = uwfClass.GetInstances();
foreach (var instance in instances)
{
Console.WriteLine(instance.ToString());
}
}
This code prints:此代码打印:
\\COMPUTER\root\standardcimv2\embedded:UWF_Servicing.CurrentSession=true
\\COMPUTER\root\standardcimv2\embedded:UWF_Servicing.CurrentSession=false
The changes can only be done to the instance where CurrentSession = false.只能对 CurrentSession = false 的实例进行更改。
How can I get this instance in a clean way?如何以干净的方式获取此实例?
In other words, I dont want to do:换句话说,我不想做:
instance.ToString().Contains("CurrentSession=false")
I believe there is a "nicer" way to do this.我相信有一种“更好”的方法可以做到这一点。 Thanks in advance!
提前致谢!
You can use SQL for WMI WHERE clause queries, something like this:您可以将SQL 用于 WMI WHERE 子句查询,如下所示:
var searcher = new ManagementObjectSearcher(
@"ROOT\StandardCimv2\embedded",
@"SELECT * FROM UWF_Servicing WHERE CurrentSession = FALSE");
foreach (ManagementObject obj in searcher.Get())
{
... etc ...
}
But you can also use the object's properties (values types will map to standard .NET's types), like this:但是您也可以使用对象的属性(值类型将映射到标准 .NET 的类型),如下所示:
var searcher = new ManagementObjectSearcher(
@"ROOT\StandardCimv2\embedded",
@"SELECT * FROM UWF_Servicing");
foreach (ManagementObject obj in searcher.Get())
{
var currentSession = obj.GetPropertyValue("CurrentSession");
if (false.Equals(currentSession))
{
... etc ...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.