/// <summary> /// 取得設備硬盤的卷標號 /// </summary> /// <returns></returns> public static string GetDiskVolumeSerialNumber() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } /// <summary> /// 得到CPU的序列號 /// </summary> /// <returns></returns> public static string getCpu() { string strCpu = null; ManagementClass myCpu = new ManagementClass("win32_Processor"); ManagementObjectCollection myCpuConnection = myCpu.GetInstances(); foreach (ManagementObject myObject in myCpuConnection) { strCpu = myObject.Properties["Processorid"].Value.ToString(); break; } return strCpu; }
而後再組合生成機器碼:html
/// <summary> /// 生成機器碼 /// </summary> /// <returns></returns> public static string getMNum() { string strNum = getCpu() + GetDiskVolumeSerialNumber();//得到24位Cpu和硬盤序列號 string strMNum = strNum.Substring(0, 24);//從生成的字符串中取出前24個字符作爲機器碼 return strMNum; } public int[] intCode = new int[127];//存儲密鑰 public int[] intNumber = new int[25];//存機器碼的Ascii值 public char[] Charcode = new char[25];//存儲機器碼字 public void setIntCode()//給數組賦值小於10的數 { for (int i = 1; i < intCode.Length; i++) { intCode[i] = i % 9; } }
生成註冊碼等:數組
/// <summary> /// 生成註冊碼 /// </summary> /// <returns></returns> public string getRNum() { setIntCode();//初始化127位數組 string MNum = getMNum();//獲取註冊碼 for (int i = 1; i < Charcode.Length; i++)//把機器碼存入數組中 { Charcode[i] = Convert.ToChar(MNum.Substring(i - 1, 1)); } for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一個整數組中。 { intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]); } string strAsciiName = "";//用於存儲註冊碼 for (int j = 1; j < intNumber.Length; j++) { if (intNumber[j] >= 48 && intNumber[j] <= 57)//判斷字符ASCII值是否0-9之間 { strAsciiName += Convert.ToChar(intNumber[j]).ToString(); } else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判斷字符ASCII值是否A-Z之間 { strAsciiName += Convert.ToChar(intNumber[j]).ToString(); } else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判斷字符ASCII值是否a-z之間 { strAsciiName += Convert.ToChar(intNumber[j]).ToString(); } else//判斷字符ASCII值不在以上範圍內 { if (intNumber[j] > 122)//判斷字符ASCII值是否大於z { strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString(); } else { strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString(); } } } return strAsciiName;//返回註冊碼 } public static string GetNetCardMACAddress() { try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))"); string NetCardMACAddress = ""; foreach (ManagementObject mo in searcher.Get()) { NetCardMACAddress = mo["MACAddress"].ToString().Trim(); } return NetCardMACAddress; } catch { return ""; } }