0

I want to make changes to the config of Microsoft Windows UWF Filter (uwfmgr.exe) via WMI in C#. Now certain changes can only be done to a specific instance of the WMI class due to their nature. 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.

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!

clamp
  • 33,000
  • 75
  • 203
  • 299
  • Have you tried something like `foreach (ManagementObject mo in new ManagementObjectSearcher("SELECT * FROM UWF_Servicing WHERE CurrentSession = false").Get())` – Simon Mourier Oct 06 '20 at 14:31
  • @SimonMourier that does work if you provide also the scope path. thanks! – clamp Oct 07 '20 at 10:34

1 Answers1

1

You can use SQL for WMI WHERE clause queries, something like this:

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:

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 ...
    }
}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298