C# 獲取CPU序列號 硬碟序列號 網絡卡ID
獲取CPU 硬碟 網絡卡ID。
/// <summary> /// 獲取cpu序列號 /// string getInfo = cm.GetCPUSerialNumber() /// if (getInfo != _info){Application.Exit();} /// </summary> /// <returns></returns> private string GetCPUSerialNumber() { string cpuSerialNumber = string.Empty; ManagementClass mc = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { cpuSerialNumber = mo["ProcessorId"].ToString(); break; } mc.Dispose(); moc.Dispose(); return cpuSerialNumber; }
/// <summary> /// 獲取硬碟序列號 static /// </summary> /// <returns></returns> private string GetDiskSerialNumber() { ManagementObjectSearcher mos = new ManagementObjectSearcher(); mos.Query = new SelectQuery("Win32_DiskDrive", "", new string[] { "PNPDeviceID", "Signature" }); ManagementObjectCollection myCollection = mos.Get(); ManagementObjectCollection.ManagementObjectEnumerator em = myCollection.GetEnumerator(); em.MoveNext(); ManagementBaseObject moo = em.Current; string id = moo.Properties["signature"].Value.ToString().Trim(); return id; }
/// <summary> /// 獲取網絡卡ID /// </summary> /// <returns></returns> private string GetMacAddressNumber() { try { string mac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) if ((bool)mo["IPEnabled"] == true) { mac += mo["MacAddress"].ToString() + " "; break; } moc = null; mc = null; return mac.Trim(); } catch (Exception e) { return e.Message + "uMnIk"; } }