C#获取WINDOWS系统信息

需引用System.Management命名空间,

具体代码如下:

 1 public class SysProp
 2     {
 3         public   SysProp()
 4         {
 5 
 6             ManagementObjectSearcher PhysicalMemory = new    ManagementObjectSearcher("select * from Win32_PhysicalMemory");
 7             ManagementObjectSearcher Processor = new ManagementObjectSearcher("select * from Win32_Processor");
 8             ManagementObjectSearcher Os = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
 9             ManagementObjectSearcher VideoController = new ManagementObjectSearcher("select * from Win32_VideoController");
10             ManagementObjectSearcher CompSys = new ManagementObjectSearcher("select * from Win32_ComputerSystem");
11 
12             PhysicalMemory_Capacity = String.Format("{0} MB", Convert.ToInt64(GetValue(PhysicalMemory, "Capacity")) / 1024 / 1024);
13 
14             ProcessorName = (string)GetValue(Processor, "Name");
15 
16             OperatingSystemBit = System.Environment.Is64BitOperatingSystem ? "64位" : "32位";
17             Os_Caption = (string)GetValue(Os, "Caption");
18             Os_Version = System.Environment.OSVersion.Version.ToString();
19             ServicePack = !String.IsNullOrEmpty(System.Environment.OSVersion.ServicePack)
20                 ? System.Environment.OSVersion.ServicePack
21                 : "";
22             SystemSpecialFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.System);
23             Video_Caption = (string)GetValue(VideoController, "Caption");
24             UserName = System.Environment.UserName;
25             UserDomainName = System.Environment.UserDomainName;
26             CompSys_Workgroup = (string)GetValue(CompSys, "Workgroup");
27         }
28 
29         private object GetValue(ManagementObjectSearcher searcher, string propName)
30         {
31             foreach (ManagementObject mobj in searcher.Get())
32                 return mobj[propName];
33             throw new NotSupportedException();
34         }
35 
36         /// <summary>
37         /// 物理内存
38         /// </summary>
39         public string PhysicalMemory_Capacity { get; set; }
40 
41         /// <summary>
42         /// 处理器
43         /// </summary>
44         public string ProcessorName { get; set; }
45 
46         /// <summary>
47         /// 处理器架构
48         /// </summary>
49         public string OperatingSystemBit { get; set; }
50 
51         /// <summary>
52         /// window名称
53         /// </summary>
54         public string Os_Caption { get; set; }
55 
56         /// <summary>
57         /// window版本
58         /// </summary>
59         public string Os_Version { get; set; }
60 
61         /// <summary>
62         /// 
63         /// </summary>
64         public string ServicePack { get; set; }
65 
66         /// <summary>
67         /// 系统目录
68         /// </summary>
69         public string SystemSpecialFolder { get; set; }
70 
71         /// <summary>
72         /// 显卡名称
73         /// </summary>
74         public string Video_Caption { get; set; }
75 
76         /// <summary>
77         /// 用户名
78         /// </summary>
79         public string UserName { get; set; }
80 
81         /// <summary>
82         /// 计算机名称
83         /// </summary>
84         public string UserDomainName { get; set; }
85 
86         /// <summary>
87         /// 工作组
88         /// </summary>
89         public string CompSys_Workgroup { get; set; }
90     }
View Code

 

posted @ 2017-07-21 11:19  编编橙  阅读(853)  评论(0编辑  收藏  举报