0

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?

andrew.fox
  • 7,435
  • 5
  • 52
  • 75

1 Answers1

1

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=@");
andrew.fox
  • 7,435
  • 5
  • 52
  • 75