C#获得硬件和操作系统各项信息
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Management;
5
6 namespace MonitorSoftware
7 {
8 internal class Computer
9 {
10 internal static string CPUName
11 {
12 get
13 {
14 string cpuinfo = "unknow";
15 ManagementClass mc = new ManagementClass("win32_processor");
16 ManagementObjectCollection moc = mc.GetInstances();
17
18 foreach (ManagementObject mo in moc)
19 {
20 cpuinfo = mo["Name"].ToString();
21 }
22 moc.Dispose();
23 return cpuinfo;
24 }
25 }
26
27 private static string CPUSN
28 {
29 get
30 {
31 string sn = "";//cpu序列号
32 ManagementClass cimobject = new ManagementClass("Win32_Processor");
33 ManagementObjectCollection moc = cimobject.GetInstances();
34 foreach (ManagementObject mo in moc)
35 {
36 sn = mo.Properties["ProcessorId"].Value.ToString();
37 }
38 return sn;
39 }
40 }
41
42 private static string HardDiskID
43 {
44 get
45 {
46 string hdid="";
47 ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
48 ManagementObjectCollection moc1 = cimobject1.GetInstances();
49 foreach (ManagementObject mo in moc1)
50 {
51 hdid = mo.Properties["Model"].Value.ToString ();
52 }
53
54 return hdid;
55 }
56 }
57
58 internal static string MAC
59 {
60 get
61 {
62 string mac = "unknow";
63 ManagementClass mAdapter = new ManagementClass("Win32_NetworkAdapterConfiguration");
64 ManagementObjectCollection moc = mAdapter.GetInstances();
65 foreach (ManagementBaseObject mo in moc)
66 {
67 if ((bool)mo["IpEnabled"] == true)
68 {
69 mac = mo["MacAddress"].ToString();
70 }
71 }
72 moc.Dispose();
73 return mac;
74 }
75
76 }
77
78 internal static string MemorySize
79 {
80 get
81 {
82 ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
83 ManagementObjectCollection moc = mc.GetInstances();
84 double sizeAll = 0.0;
85 foreach (ManagementObject mo in moc)
86 {
87 if (mo.Properties["TotalVisibleMemorySize"].Value != null)
88 {
89 sizeAll += Convert.ToDouble(mo.Properties["TotalVisibleMemorySize"].Value.ToString());
90 }
91 }
92 //sizeAll /= 1000000;
93 moc.Dispose();
94 return sizeAll.ToString("0.###");
95 }
96
97 }
98
99 internal static string[] IPAddress
100 {
101 get
102 {
103 string hostname = System.Net.Dns.GetHostName();
104 System.Net.IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(hostname);
105 string[] ips = new string[ipEntry.AddressList.Length];
106 for (int i = 0; i < ipEntry.AddressList.Length; i++)
107 {
108 ips[i] = ipEntry.AddressList[i].ToString();
109 }
110 return ips;
111 }
112 }
113
114 internal static string OSType
115 {
<span style="color%