2

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??

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Harry Boy
  • 4,159
  • 17
  • 71
  • 122

2 Answers2

1

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

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

string _operatingSystem = Environment.OSVersion.VersionString;
Douglas
  • 53,759
  • 13
  • 140
  • 188
  • THis gives me "Microsoft Windows NT 6.1.7601 Service Pack 1" but I'm running Windows 7 which is what I want to display to the user. – Harry Boy Jan 12 '14 at 18:58
  • At least for 8.1(+) Environment.OSVersion.VersionString doesn't necessary display the running OS. (Rather its influenced by the C# application's manifest) – Tom Nov 20 '15 at 19:36
  • could you justify your suggestion? – Shekhar Reddy Apr 13 '18 at 15:36