C# 获取机器码Code
Code
1using System;
2using System.Management;
3using System.IO;
4using System.Runtime.InteropServices;
5using System.Security.Cryptography;
6using System.Text;
7using Microsoft.Win32;
8
9namespace CHUmanager.Common
10{
11 /**//// <summary>
12 /// register 的摘要说明。
13 /// 注册软件所用到的类
16 /// </summary>
17 public class Register
18 {
19
20 获取cpu序列号 硬盘ID 网卡硬地址#region 获取cpu序列号 硬盘ID 网卡硬地址
21 /**//// <summary>
22 /// 获取cpu序列号
23 /// </summary>
24 /// <returns>string </returns>
25 public string GetCpuInfo()
26 {
27 string cpuInfo = "";
28 ManagementClass cimobject = new ManagementClass("Win32_Processor");
29 ManagementObjectCollection moc = cimobject.GetInstances();
30 foreach (ManagementObject mo in moc)
31 {
32 cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
33 }
34 return cpuInfo.ToString();
35 }
36
37 /**//// <summary>
38 /// 获取硬盘ID
39 /// </summary>
40 /// <returns>string </returns>
41 public string GetHDid()
42 {
43 string HDid = "";
44 ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
45 ManagementObjectCollection moc1 = cimobject1.GetInstances();
46 foreach (ManagementObject mo in moc1)
47 {
48 HDid = (string)mo.Properties["Model"].Value;
49 }
50 return HDid.ToString();
51 }
52
53 /**//// <summary>
54 /// 获取网卡硬件地址
55 /// </summary>
56 /// <returns>string </returns>
57 public string GetMoAddress()
58 {
59 string MoAddress = "";
60 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
61 ManagementObjectCollection moc2 = mc.GetInstances();
62 foreach (ManagementObject mo in moc2)
63 {
64 if ((bool)mo["IPEnabled"] == true)
65 MoAddress = mo["MacAddress"].ToString();
66 mo.Dispose();
67 }
68 return MoAddress.ToString();
69 }
70 #endregion
71 }
72}
1using System;
2using System.Management;
3using System.IO;
4using System.Runtime.InteropServices;
5using System.Security.Cryptography;
6using System.Text;
7using Microsoft.Win32;
8
9namespace CHUmanager.Common
10{
11 /**//// <summary>
12 /// register 的摘要说明。
13 /// 注册软件所用到的类
16 /// </summary>
17 public class Register
18 {
19
20 获取cpu序列号 硬盘ID 网卡硬地址#region 获取cpu序列号 硬盘ID 网卡硬地址
21 /**//// <summary>
22 /// 获取cpu序列号
23 /// </summary>
24 /// <returns>string </returns>
25 public string GetCpuInfo()
26 {
27 string cpuInfo = "";
28 ManagementClass cimobject = new ManagementClass("Win32_Processor");
29 ManagementObjectCollection moc = cimobject.GetInstances();
30 foreach (ManagementObject mo in moc)
31 {
32 cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
33 }
34 return cpuInfo.ToString();
35 }
36
37 /**//// <summary>
38 /// 获取硬盘ID
39 /// </summary>
40 /// <returns>string </returns>
41 public string GetHDid()
42 {
43 string HDid = "";
44 ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
45 ManagementObjectCollection moc1 = cimobject1.GetInstances();
46 foreach (ManagementObject mo in moc1)
47 {
48 HDid = (string)mo.Properties["Model"].Value;
49 }
50 return HDid.ToString();
51 }
52
53 /**//// <summary>
54 /// 获取网卡硬件地址
55 /// </summary>
56 /// <returns>string </returns>
57 public string GetMoAddress()
58 {
59 string MoAddress = "";
60 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
61 ManagementObjectCollection moc2 = mc.GetInstances();
62 foreach (ManagementObject mo in moc2)
63 {
64 if ((bool)mo["IPEnabled"] == true)
65 MoAddress = mo["MacAddress"].ToString();
66 mo.Dispose();
67 }
68 return MoAddress.ToString();
69 }
70 #endregion
71 }
72}