中如何获取本机MAC地址、IP地址、硬盘ID、CPU序列号等系统信息

我们在利用C#开发桌面程序(Winform)程序的时候,经常需要获取一些跟系统相关的信息,例如用户名、MAC地址、IP地址、硬盘ID、CPU序列号、系统名称、物理内存等。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using System.Management;  
/// 获取本机用户名、MAC地址、内网IP地址、公网IP地址、硬盘ID、CPU序列号、系统名称、物理内存。
/// </summary>
public class GetSystemInfo
{
/// <summary>
/// 操作系统的登录用户名
/// </summary>
/// <returns>系统的登录用户名</returns>
public static string GetUserName()
{
try
{
string strUserName = string.Empty;
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
strUserName = mo["UserName"].ToString();
}
moc = null;
mc = null;
return strUserName;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取本机MAC地址
/// </summary>
/// <returns>本机MAC地址</returns>
public static string GetMacAddress()
{
try
{
string strMac = string.Empty;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"] == true)
{
strMac = mo["MacAddress"].ToString();
}
}
moc = null;
mc = null;
return strMac;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取本机的物理地址
/// </summary>
/// <returns></returns>
public static string getMacAddr_Local()
{
string madAddr = null;
try
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc2 = mc.GetInstances();
foreach (ManagementObject mo in moc2)
{
if (Convert.ToBoolean(mo["IPEnabled"]) == true)
{
madAddr = mo["MacAddress"].ToString();
madAddr = madAddr.Replace(':', '-');
}
mo.Dispose();
}
if (madAddr == null)
{
return "unknown";
}
else
{
return madAddr;
}
}
catch (Exception)
{
return "unknown";
}
}
/// <summary>
/// 获取客户端内网IPv6地址
/// </summary>
/// <returns>客户端内网IPv6地址</returns>
public static string GetClientLocalIPv6Address()
{
string strLocalIP = string.Empty;
try
{
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHost.AddressList[0];
strLocalIP = ipAddress.ToString();
return strLocalIP;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取客户端内网IPv4地址
/// </summary>
/// <returns>客户端内网IPv4地址</returns>
public static string GetClientLocalIPv4Address()
{
string strLocalIP = string.Empty;
try
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHost.AddressList[0];
strLocalIP = ipAddress.ToString();
return strLocalIP;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取客户端内网IPv4地址集合
/// </summary>
/// <returns>返回客户端内网IPv4地址集合</returns>
public static List<string> GetClientLocalIPv4AddressList()
{
List<string> ipAddressList = new List<string>();
try
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
foreach (IPAddress ipAddress in ipHost.AddressList)
{
if (!ipAddressList.Contains(ipAddress.ToString()))
{
ipAddressList.Add(ipAddress.ToString());
}
}
}
catch
{

}
return ipAddressList;
}

/// <summary>
/// 获取客户端外网IP地址
/// </summary>
/// <returns>客户端外网IP地址</returns>
public static string GetClientInternetIPAddress()
{
string strInternetIPAddress = string.Empty;
try
{
using (WebClient webClient = new WebClient())
{
strInternetIPAddress = webClient.DownloadString("http://www.coridc.com/ip");
Regex r = new Regex("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}");
Match mth = r.Match(strInternetIPAddress);
if (!mth.Success)
{
strInternetIPAddress = GetClientInternetIPAddress2();
mth = r.Match(strInternetIPAddress);
if (!mth.Success)
{
strInternetIPAddress = "unknown";
}
}
return strInternetIPAddress;
}
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取本机公网IP地址
/// </summary>
/// <returns>本机公网IP地址</returns>
private static string GetClientInternetIPAddress2()
{
string tempip = "";
try
{
//http://iframe.ip138.com/ic.asp 返回的是:您的IP是:[220.231.17.99] 来自:北京市 光环新网
WebRequest wr = WebRequest.Create("http://iframe.ip138.com/ic.asp");
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据

int start = all.IndexOf("[") + 1;
int end = all.IndexOf("]", start);
tempip = all.Substring(start, end - start);
sr.Close();
s.Close();
return tempip;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取硬盘序号
/// </summary>
/// <returns>硬盘序号</returns>
public static string GetDiskID()
{
try
{
string strDiskID = string.Empty;
ManagementClass mc = new ManagementClass("Win32_DiskDrive");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
strDiskID = mo.Properties["Model"].Value.ToString();
}
moc = null;
mc = null;
return strDiskID;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取CpuID
/// </summary>
/// <returns>CpuID</returns>
public static string GetCpuID()
{
try
{
string strCpuID = string.Empty;
ManagementClass mc = new ManagementClass("Win32_Processor");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
strCpuID = mo.Properties["ProcessorId"].Value.ToString();
}
moc = null;
mc = null;
return strCpuID;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取操作系统类型
/// </summary>
/// <returns>操作系统类型</returns>
public static string GetSystemType()
{
try
{
string strSystemType = string.Empty;
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
strSystemType = mo["SystemType"].ToString();
}
moc = null;
mc = null;
return strSystemType;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取操作系统名称
/// </summary>
/// <returns>操作系统名称</returns>
public static string GetSystemName()
{
try
{
string strSystemName = string.Empty;
ManagementObjectSearcher mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT PartComponent FROM Win32_SystemOperatingSystem");
foreach (ManagementObject mo in mos.Get())
{
strSystemName = mo["PartComponent"].ToString();
}
mos = new ManagementObjectSearcher("root\\CIMV2", "SELECT Caption FROM Win32_OperatingSystem");
foreach (ManagementObject mo in mos.Get())
{
strSystemName = mo["Caption"].ToString();
}
return strSystemName;
}
catch
{
return "unknown";
}
}
/// <summary>
/// 获取物理内存信息
/// </summary>
/// <returns>物理内存信息</returns>
public static string GetTotalPhysicalMemory()
{
try
{
string strTotalPhysicalMemory = string.Empty;
ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
strTotalPhysicalMemory = mo["TotalPhysicalMemory"].ToString();
}
moc = null;
mc = null;
return strTotalPhysicalMemory;
}
catch
{
return "unknown";
}
}

/// <summary>
/// 获取主板id
/// </summary>
/// <returns></returns>
public static string GetMotherBoardID()
{
try
{
ManagementClass mc = new ManagementClass("Win32_BaseBoard");
ManagementObjectCollection moc = mc.GetInstances();
string strID = null;
foreach (ManagementObject mo in moc)
{
strID = mo.Properties["SerialNumber"].Value.ToString();
break;
}
return strID;
}
catch
{
return "unknown";
}
}

/// <summary>
/// 获取公用桌面路径
public static string GetAllUsersDesktopFolderPath()
{
RegistryKey folders;
folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
string desktopPath = folders.GetValue("Common Desktop").ToString();
return desktopPath;
}
/// <summary>
/// 获取公用启动项路径
/// </summary>
public static string GetAllUsersStartupFolderPath()
{
RegistryKey folders;
folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
string Startup = folders.GetValue("Common Startup").ToString();
return Startup;
}
private static RegistryKey OpenRegistryPath(RegistryKey root, string s)
{
s = s.Remove(0, 1) + @"/";
while (s.IndexOf(@"/") != -1)
{
root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"/")));
s = s.Remove(0, s.IndexOf(@"/") + 1);
}
return root;
}
private void Test()
{
RegistryKey folders;
folders = OpenRegistryPath(Registry.LocalMachine, @"/software/microsoft/windows/currentversion/explorer/shell folders");
// Windows用户桌面路径
string desktopPath = folders.GetValue("Common Desktop").ToString();
// Windows用户字体目录路径
string fontsPath = folders.GetValue("Fonts").ToString();
// Windows用户网络邻居路径
string nethoodPath = folders.GetValue("Nethood").ToString();
// Windows用户我的文档路径
string personalPath = folders.GetValue("Personal").ToString();
// Windows用户开始菜单程序路径
string programsPath = folders.GetValue("Programs").ToString();
// Windows用户存放用户最近访问文档快捷方式的目录路径
string recentPath = folders.GetValue("Recent").ToString();
// Windows用户发送到目录路径
string sendtoPath = folders.GetValue("Sendto").ToString();
// Windows用户开始菜单目录路径
string startmenuPath = folders.GetValue("Startmenu").ToString();
// Windows用户开始菜单启动项目录路径
string startupPath = folders.GetValue("Startup").ToString();
// Windows用户收藏夹目录路径
string favoritesPath = folders.GetValue("Favorites").ToString();
// Windows用户网页历史目录路径
string historyPath = folders.GetValue("History").ToString();
// Windows用户Cookies目录路径
string cookiesPath = folders.GetValue("Cookies").ToString();
// Windows用户Cache目录路径
string cachePath = folders.GetValue("Cache").ToString();
// Windows用户应用程式数据目录路径
string appdataPath = folders.GetValue("Appdata").ToString();
// Windows用户打印目录路径
string printhoodPath = folders.GetValue("Printhood").ToString();
}</span>