using System; using System.Management; namespace DownKyi.Core.Utils { public class MachineCode { private static MachineCode machineCode; /// /// 获取机器码 /// /// public static string GetMachineCodeString() { if (machineCode == null) { machineCode = new MachineCode(); } string machineCodeString = "PC." + machineCode.GetMainBordId() + "." + machineCode.GetCpuInfo() + "." + machineCode.GetDiskID();// + "." + //machineCode.GetMoAddress(); return machineCodeString.Replace(" ", ""); } /// /// 获取主板ID /// /// public string GetMainBordId() { string strId = ""; try { using (ManagementClass mc = new ManagementClass("Win32_BaseBoard")) { ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { strId = mo.Properties["SerialNumber"].Value.ToString(); mo.Dispose(); break; } } } catch (Exception) { return "unknown"; //throw; } return strId; } /// /// 获取cpu序列号 /// /// public string GetCpuInfo() { string cpuInfo = ""; try { using (ManagementClass cimobject = new ManagementClass("Win32_Processor")) { ManagementObjectCollection moc = cimobject.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); mo.Dispose(); } } } catch (Exception) { return "unknown"; //throw; } return cpuInfo; } /// /// 获取硬盘ID /// /// public string GetDiskID() { string diskName = ""; string diskID = ""; try { using (ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive")) { ManagementObjectCollection moc1 = cimobject1.GetInstances(); foreach (ManagementObject mo in moc1) { diskName = mo.Properties["Model"].Value.ToString(); diskID = mo.Properties["SerialNumber"].Value.ToString(); mo.Dispose(); } } } catch (Exception) { return "unknown"; //throw; } return diskName + diskID; } /// /// 获取网卡硬件地址 /// /// public string GetMoAddress() { string MoAddress = ""; try { using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration")) { ManagementObjectCollection moc2 = mc.GetInstances(); foreach (ManagementObject mo in moc2) { if ((bool)mo["IPEnabled"]) { MoAddress = mo["MacAddress"].ToString(); } mo.Dispose(); } } } catch (Exception) { return "unknown"; //throw; } return MoAddress; } } }