Get the operating system info by using the classes from namespace System.Management. OS: Windows XP, Vista, Win7, Windows Server 2008 and so on.
1 using System;
2 using System.Management;
3
4 namespace GetOSInfo_Peter
5 {
6 class Program
7 {
8 static void Main(string[] args)
9 {
10 SelectQuery query = new SelectQuery("Win32_operatingSystem");
11 ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
12 ManagementObjectCollection objs = searcher.Get();
13
14 foreach (ManagementObject obj in objs)
15 {
16 Console.WriteLine(obj.GetPropertyValue("Caption").ToString());
17 Console.WriteLine(obj.GetPropertyValue("BootDevice").ToString());
18 Console.WriteLine(obj.GetPropertyValue("BuildNumber").ToString());
19 Console.WriteLine(obj.GetPropertyValue("CSDVersion").ToString());
20 Console.WriteLine(obj.GetPropertyValue("Manufacturer").ToString());
21 Console.WriteLine(obj.GetPropertyValue("Version").ToString());
22 Console.WriteLine(obj.GetPropertyValue("SystemDirectory").ToString());
23 break;
24 }
25 }
26 }
27 }
Output:
Output all the info by "Console.WriteLine(obj.GetText(TextFormat.Mof))".
The os info can be found in "wbemtest". Type "wbemtest" in 'Run', then connect to "root\cimv2" namespace, "Enum Classes..." -> select "Recursive" on 'Superclass Info' dialogue -> "OK" -> select the "Win32_OperatingSystem" in the 'Query Result' -> click "Instances" on the 'Object editor for Win32_OperatingSystem' -> open the instance and "Show MOF".
namespace: System.Management
[MSDN]Provides access to a rich set of management information and management events about the system, devices, and applications instrumented to the Windows Management Instrumentation (WMI) infrastructure.Applications and services can query for interesting management information (such as how much free space is left on the disk, what is the current CPU utilization, which database a certain application is connected to, and much more), using classes derived from ManagementObjectSearcher and ManagementQuery, or subscribe to a variety of management events using the ManagementEventWatcher class.The accessible data can be from both managed and unmanaged components in the distributed environment.
The classes that are used in the above source code:
SelectQuery Represents a WQL SELECT data query.
ManagementObjectSearcher Retrieves a collection of management objects based on a specified query.
ManagementObjectCollection Represents different collections of management objects retrieved through WMI.
ManagementObject Represents a WMI instance.