提供使用者硬體資訊輔助類HardwareHelper
摘要:
實現效果
本輔助類主要是用來方便實現獲取使用者硬體的相關資訊,根據使用者各種硬體(CPU、網絡卡、磁碟、主機板等)標識資訊,獲取一個8位唯一標識數字。
本輔助類只提供了一個公共的方法Value,它集合了CPU、網絡卡、磁碟、主機板等資訊,通過這個8位的數字...
實現效果
- 本輔助類主要是用來方便實現獲取使用者硬體的相關資訊,根據使用者各種硬體(CPU、網絡卡、磁碟、主機板等)標識資訊,獲取一個8位唯一標識數字。
- 本輔助類只提供了一個公共的方法Value,它集合了CPU、網絡卡、磁碟、主機板等資訊,通過這個8位的數字標識,我們可以唯一區分一臺電腦,類似我們每個人的指紋資訊一樣,很少會發生重複的現象,可以用在一些關鍵的註冊授權機制上。
實現步驟
- 在程式碼引用相關的程式碼實現動態呼叫
/// 根據使用者各種硬體(CPU、網絡卡、磁碟、主機板等)標識資訊,獲取一個8位唯一標識數字 public static string Value() /// 獲取硬體的標識資訊 private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) /// 獲取硬體的標識資訊 private static string identifier(string wmiClass, string wmiProperty) /// 獲取CPU資訊 private static string cpuId() /// 獲取BOIS主機板標識資訊 private static string biosId() /// 獲取主硬碟的標識資訊 private static string diskId() /// 獲取主機板的標識資訊 private static string baseId() /// 獲取主視訊控制器標識資訊 private static string videoId() /// 獲取第一個可用網絡卡資訊 private static string macId() /// 轉換內容為8位數字 private static string pack(string text)
- 輔助類HardwareHelper的使用例子程式碼如下所示
string identity = HardwareHelper.Value(); MessageUtil.ShowTips(identity);
附上原始碼
/// <summary> /// 提供使用者硬體唯一資訊的輔助類 /// </summary> public class HardwareHelper { /// <summary> /// 根據使用者各種硬體(CPU、網絡卡、磁碟、主機板等)標識資訊, /// 獲取一個8位唯一標識數字 /// </summary> public static string Value() { return pack(cpuId() + biosId() + diskId() + baseId() + videoId() + macId()); } /// <summary> /// 獲取硬體的標識資訊 /// </summary> private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue) { string result=""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { if (mo[wmiMustBeTrue].ToString()=="True") { //Only get the first one if (result=="") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } } return result; } /// <summary> /// 獲取硬體的標識資訊 /// </summary> private static string identifier(string wmiClass, string wmiProperty) { string result=""; System.Management.ManagementClass mc = new System.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollection moc = mc.GetInstances(); foreach (System.Management.ManagementObject mo in moc) { //Only get the first one if (result=="") { try { result = mo[wmiProperty].ToString(); break; } catch { } } } return result; } /// <summary> /// 獲取CPU資訊 /// </summary> private static string cpuId() { //Uses first CPU identifier available in order of preference //Don't get all identifiers, as very time consuming string retVal = identifier("Win32_Processor", "UniqueId"); if (retVal=="") //If no UniqueID, use ProcessorID { retVal = identifier("Win32_Processor", "ProcessorId"); if (retVal=="") //If no ProcessorId, use Name { retVal = identifier("Win32_Processor", "Name"); if (retVal=="") //If no Name, use Manufacturer { retVal = identifier("Win32_Processor", "Manufacturer"); } //Add clock speed for extra security retVal +=identifier("Win32_Processor", "MaxClockSpeed"); } } return retVal; } /// <summary> /// 獲取BOIS主機板標識資訊 /// </summary> private static string biosId() { return identifier("Win32_BIOS", "Manufacturer") + identifier("Win32_BIOS", "SMBIOSBIOSVersion") + identifier("Win32_BIOS", "IdentificationCode") + identifier("Win32_BIOS", "SerialNumber") + identifier("Win32_BIOS", "ReleaseDate") + identifier("Win32_BIOS", "Version"); } /// <summary> /// 獲取主硬碟的標識資訊 /// </summary> /// <returns></returns> private static string diskId() { return identifier("Win32_DiskDrive", "Model") + identifier("Win32_DiskDrive", "Manufacturer") + identifier("Win32_DiskDrive", "Signature") + identifier("Win32_DiskDrive", "TotalHeads"); } /// <summary> /// 獲取主機板的標識資訊 /// </summary> /// <returns></returns> private static string baseId() { return identifier("Win32_BaseBoard", "Model") + identifier("Win32_BaseBoard", "Manufacturer") + identifier("Win32_BaseBoard", "Name") + identifier("Win32_BaseBoard", "SerialNumber"); } /// <summary> /// 獲取主視訊控制器標識資訊 /// </summary> /// <returns></returns> private static string videoId() { return identifier("Win32_VideoController", "DriverVersion") + identifier("Win32_VideoController", "Name"); } /// <summary> /// 獲取第一個可用網絡卡資訊 /// </summary> /// <returns></returns> private static string macId() { return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled"); } /// <summary> /// 轉換內容為8位數字 /// </summary> private static string pack(string text) { string retVal; int x = 0; int y = 0; foreach (char n in text) { y++; x += (n*y); } retVal = x.ToString() + "00000000"; return retVal.Substring(0, 8); } }