Get the Type of the OS(Windows)

  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.

转载于:https://www.cnblogs.com/Peter-Zhang/archive/2011/09/17/2179590.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值