winform取CPU编号、MAC地址、硬盘信息、IP地址、串口信息
using System.Management; //需要在解决方案中引用System.Management.DLL文件
1 /// <summary>
2 /// 取CPU编号
3 /// </summary>
4 /// <returns></returns>
5 public static string GetCpuID()
6 {
7 try
8 {
9 ManagementClass mc = new ManagementClass("Win32_Processor");
10 ManagementObjectCollection moc = mc.GetInstances();
11 string strCpuID = null;
12 foreach (ManagementObject mo in moc)
13 {
14 strCpuID = mo.Properties["ProcessorId"].Value.ToString();
15 break;
16 }
17 return strCpuID;
18 }
19 catch
20 {
21 return "";
22 }
23 }
24
25 /// <summary>
26 /// 取第一块硬盘编号
27 /// </summary>
28 /// <returns></returns>
29 public static string GetHardDiskID()
30 {
31 try
32 {
33 ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
34 string strHardDiskID = null;
35 foreach (ManagementObject mo in searcher.Get())
36 {
37 strHardDiskID = mo["SerialNumber"].ToString().Trim();
38 break;
39 }
40 return strHardDiskID;
41 }
42 catch
43 {
44 return "";
45 }
46 }
47
48 /// <summary>
49 /// 获取网卡MAC地址
50 /// </summary>
51 /// <returns></returns>
52 public static string GetNetCardMAC()
53 {
54 try
55 {
56 string stringMAC = "";
57 ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
58 ManagementObjectCollection MOC = MC.GetInstances();
59 foreach (ManagementObject MO in MOC)
60 {
61 if ((bool)MO["IPEnabled"] == true)
62 {
63 stringMAC += MO["MACAddress"].ToString();
64
65 }
66 }
67 return stringMAC;
68 }
69 catch
70 {
71 return "";
72 }
73 }
74
75 /// <summary>
76 /// 获取硬盘信息的代码
77 /// </summary>
78 /// <param name="drvID"></param>
79 /// <returns></returns>
80 public static string GetVolOf(string drvID)
81 {
82 try
83 {
84 const int MAX_FILENAME_LEN = 256;
85 int retVal = 0;
86 int a = 0;
87 int b = 0;
88 string str1 = null;
89 string str2 = null;
90 int i = GetVolumeInformation(drvID + @":/", str1, MAX_FILENAME_LEN, ref retVal, a, b, str2, MAX_FILENAME_LEN);
91 return retVal.ToString("x");
92 }
93 catch
94 {
95 return "";
96 }
97 }
98
99 /// <summary>
100 /// 获取当前网卡IP地址
101 /// </summary>
102 /// <returns></returns>
103 public static string GetNetCardIP()
104 {
105 try
106 {
107 string stringIP = "";
108 ManagementClass MC = new ManagementClass("Win32_NetworkAdapterConfiguration");
109 ManagementObjectCollection MOC = MC.GetInstances();
110 foreach (ManagementObject MO in MOC)
111 {
112 if ((bool)MO["IPEnabled"] == true)
113 {
114 string[] IPAddresses = (string[])MO["IPAddress"];
115 if (IPAddresses.Length > 0)
116 {
117 stringIP = IPAddresses[0].ToString();
118 }
119 }
120 }
121 return stringIP;
122 }
123 catch
124 {
125 return "";
126 }
127 }
128
129 #region 调用注册表返回本地串口
130 /// <summary>
131 /// 串口函数(方法需调用注册表,串口编程所用类)
132 /// </summary>
133 /// 使用命名空间:
134 /// using System.Security;
135 /// using System.Security.Permissions;
136 /// <returns>返回此计算机串口数组</returns>
137 public static string[] GetPortNames()//
138 {
139 RegistryKey localMachine = null;
140 RegistryKey key2 = null;
141 string[] textArray = null;//这里有个判断,判断该注册表项是否存在
142 new RegistryPermission(RegistryPermissionAccess.Read, @"HKEY_LOCAL_MACHINE/HARDWARE/DEVICEMAP/SERIALCOMM").Assert();
143 try
144 {
145 localMachine = Registry.LocalMachine;
146 key2 = localMachine.OpenSubKey(@"HARDWARE/DEVICEMAP/SERIALCOMM", false);
147 if (key2 != null)
148 {
149 string[] valueNames = key2.GetValueNames();
150 textArray = new string[valueNames.Length];
151 for (int i = 0; i < valueNames.Length; i++)
152 {
153 textArray[i] = (string)key2.GetValue(valueNames[i]);
154 }
155 }
156 }
157 finally
158 {
159 if (localMachine != null)
160 {
161 localMachine.Close();
162 } if (key2 != null)
163 {
164 key2.Close();
165 }
166 CodeAccessPermission.RevertAssert();
167 } if (textArray == null)
168 {
169 textArray = new string[0];
170 }
171 return textArray;
172 }
173 #endregion
精益求精!
浙公网安备 33010602011771号