select * from Win32_OperatingSystem throwing exception

2.8k views Asked by At

I hace a WPF application and I'm am using the following to obtain the OS details of my PC:

using (ManagementObjectSearcher win32OperatingSystem = new ManagementObjectSearcher("select * from Win32_OperatingSystem"))
{
     foreach (ManagementObject obj in win32OperatingSystem.Get())
     {
         _operatingSystem = obj["Caption"].ToString();
         _osArchitecture = obj["OSArchitecture"].ToString();
         break;
     }
}

WhenI step into this line:

ManagementObject obj in win32OperatingSystem.Get())

I get the following exception:

Invalid Query.

WHat is wrong here??

2

There are 2 answers

3
Douglas On

I would suggest using the properties of the Environment and OperatingSystem class (instead of ManagementObjectSearcher) to get those details.

string _operatingSystem = Environment.OSVersion.VersionString;
0
Hans Passant On

WMI classes have a namespace, you didn't specify one. Use WMI Code Creator to get your queries correct, it generates the C# code for you and you can run it right from the tool to check the results.