C#读取CPU序列号、硬盘ID、网卡MAC地址,生成机器码

话不多说,直接上代码,类库中的机器码使用序列号、硬盘ID、网卡MAC地址组合取MD5生成。

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
using System;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;

namespace WayneShao.Common
{
internal static class MachineCode
{
private static string _machineCodeString;
public static string Value
{
get
{
if (string.IsNullOrWhiteSpace(_machineCodeString))
_machineCodeString = GetMD5($"{CPUCode}_{HDId}_{MacAddress}");
return _machineCodeString;
}
}

private static string _cpuCode;
public static string CPUCode => _cpuCode ?? (_cpuCode = GetCPUInfo());

private static string _hdId;
public static string HDId => _hdId ?? (_hdId = GetHDid());

private static string _macAddress;
public static string MacAddress => _macAddress ?? (_macAddress = GetMACAddress());

/// <summary>
/// 获取cpu序列号
/// </summary>
/// <returns> string </returns>
private static string GetCPUInfo()
{
using (var cimobject = new ManagementClass("Win32_Processor"))
{
var hdids = cimobject.GetInstances().Cast<ManagementObject>().Select(o => o.Properties["ProcessorId"].Value).Cast<string>().ToArray();
return hdids.Any() ? hdids.First() : string.Empty;
}
}

/// <summary>
/// 获取硬盘ID
/// </summary>
/// <returns> string </returns>
private static string GetHDid()
{
using (var cimobject1 = new ManagementClass("Win32_DiskDrive"))
{
var hdids = cimobject1.GetInstances().Cast<ManagementObject>().Select(o => o.Properties["Model"].Value).Cast<string>().ToArray();
return hdids.Any() ? hdids.First() : string.Empty;
}
}

/// <summary>
/// 获取网卡硬件地址
/// </summary>
/// <returns> string </returns>
private static string GetMACAddress()
{
using (var mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
var macs = mc.GetInstances().Cast<ManagementObject>().Where(o => (bool)o["IPEnabled"]).Select(o => o["MacAddress"].ToString()).ToArray();
return macs.Any() ? macs.First() : string.Empty;
}
}

/// <summary>
/// 获取字符串的MD5值
/// </summary>
/// <returns> string </returns>
public static string GetMD5(string source)
{
var result = Encoding.Default.GetBytes(source);
var md5 = new MD5CryptoServiceProvider();
var output = md5.ComputeHash(result);
return BitConverter.ToString(output).Replace("-", "").ToLower();
}

}
}

另附MSDN中关于 WMI Class 的文档供大家参考
https://msdn.microsoft.com/zh-cn/library/aa394173(VS.85).aspx

欢迎关注我的其它发布渠道