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?
There is a special @ operator in WMI query to extract singletons. It can be used like this: