右键当前程序集下的“引用”,选择添加引用,在“.NET”选项卡中选择System.Management,点击确定即可!
下面是获取MAC地址代码:
private static string GetMac() { string mac = ""; ManagementClass mcMAC = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection mocMAC = mcMAC.GetInstances(); foreach (ManagementObject m in mocMAC) { if ((bool)m["IPEnabled"]) { mac= m["MacAddress"].ToString(); } } return mac; }
下面是获取CPU序列号代码:
public string GetCpuID() { try { // 获取CPU序列号代码 string cpuInfo = "";//cpu序列号 ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); } moc = null; mc = null; return cpuInfo; } catch { return "unknow"; } finally { } }