.NET中获取CPU编号及MAC地址
首先要引用 System.Management.dll
命名空间: using System.Management;
1 //取CPU编号
2 public string GetCpuID()
3 {
4 try
5 {
6 ManagementClass mc = new ManagementClass("Win32_Processor");
7 ManagementObjectCollection moc = mc.GetInstances();
8
9 string strCpuID = null;
10 foreach (ManagementObject mo in moc)
11 {
12 strCpuID = mo.Properties["ProcessorId"].Value.ToString();
13 break;
14 }
15 return strCpuID;
16 }
17 catch
18 {
19 return "";
20 }
21
22 }
23
24 //获取网卡mac地址
25 public string GetMac()
26 {
27 try
28 {
29 ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
30 ManagementObjectCollection queryCollection = query.Get();
31 foreach (ManagementObject mo in queryCollection)
32 {
33 if (mo["IPEnabled"].ToString() == "True")
34 return mo["MacAddress"].ToString();
35 }
36 return "";
37 }
38 catch
39 {
40 return "";
41 }
42 }
2 public string GetCpuID()
3 {
4 try
5 {
6 ManagementClass mc = new ManagementClass("Win32_Processor");
7 ManagementObjectCollection moc = mc.GetInstances();
8
9 string strCpuID = null;
10 foreach (ManagementObject mo in moc)
11 {
12 strCpuID = mo.Properties["ProcessorId"].Value.ToString();
13 break;
14 }
15 return strCpuID;
16 }
17 catch
18 {
19 return "";
20 }
21
22 }
23
24 //获取网卡mac地址
25 public string GetMac()
26 {
27 try
28 {
29 ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
30 ManagementObjectCollection queryCollection = query.Get();
31 foreach (ManagementObject mo in queryCollection)
32 {
33 if (mo["IPEnabled"].ToString() == "True")
34 return mo["MacAddress"].ToString();
35 }
36 return "";
37 }
38 catch
39 {
40 return "";
41 }
42 }