Selecting a single instance by using WMI query

663 views Asked by At

I need to query the WMI for an object that I know to be a singleton (there is only one instance). I used to do that by a loop, but it doesn't look good:

var searcher = new ManagementObjectSearcher("root\\CIMV2\\Security\\MicrosoftTpm",
    "SELECT * FROM Win32_Tpm", null);

foreach (ManagementObject classInstance in searcher.Get())
{
    // use classInstance here
    break;
}

How to do it better?

1

There are 1 answers

0
andrew.fox On BEST ANSWER

There is a special @ operator in WMI query to extract singletons. It can be used like this:

ManagementObject classInstance = new ManagementObject("root\\CIMV2\\Security\\MicrosoftTpm:Win32_Tpm=@");