C# 读取机器码,CPU序列号,生成注册码类(优化)
有些CPU不支持获取序列号。
获取strCpu = myObject.Properties[
"Processorid"
].Value.ToString();
时,请判断下返回是否为null,为null的话就说明cpu不支持。 获取集合对象属性,除非100%确定,否则不要直接访问属性。一旦返回空,就会报空引用错误
代码 using System.Management;//需要在项目中添加System.Management引用 namespace ECBC_CDKEY { public class SoftReg { /// <summary> /// 取得设备硬盘的卷标号 /// </summary> /// <returns></returns> public 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 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; } /// <summary> /// 生成机器码 /// </summary> /// <returns></returns> public 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 = this.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;//返回注册码 } } }
1 代码 2 3 4 using System.Management;//需要在项目中添加System.Management引用 5 6 namespace ECBC_CDKEY 7 { 8 public class SoftReg 9 { 10 /// <summary> 11 /// 取得设备硬盘的卷标号 12 /// </summary> 13 /// <returns></returns> 14 public string GetDiskVolumeSerialNumber() 15 { 16 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 17 ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); 18 disk.Get(); 19 return disk.GetPropertyValue("VolumeSerialNumber").ToString(); 20 } 21 22 /// <summary> 23 /// 获得CPU的序列号 24 /// </summary> 25 /// <returns></returns> 26 public string getCpu() 27 { 28 string strCpu = null; 29 ManagementClass myCpu = new ManagementClass("win32_Processor"); 30 ManagementObjectCollection myCpuConnection = myCpu.GetInstances(); 31 foreach (ManagementObject myObject in myCpuConnection) 32 { 33 strCpu = myObject.Properties["Processorid"].Value.ToString(); 34 break; 35 } 36 return strCpu; 37 } 38 39 /// <summary> 40 /// 生成机器码 41 /// </summary> 42 /// <returns></returns> 43 public string getMNum() 44 { 45 string strNum = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号 46 string strMNum = strNum.Substring(0, 24);//从生成的字符串中取出前24个字符做为机器码 47 return strMNum; 48 } 49 public int[] intCode = new int[127];//存储密钥 50 public int[] intNumber = new int[25];//存机器码的Ascii值 51 public char[] Charcode = new char[25];//存储机器码字 52 public void setIntCode()//给数组赋值小于10的数 53 { 54 for (int i = 1; i < intCode.Length; i++) 55 { 56 intCode[i] = i % 9; 57 } 58 } 59 60 /// <summary> 61 /// 生成注册码 62 /// </summary> 63 /// <returns></returns> 64 public string getRNum() 65 { 66 setIntCode();//初始化127位数组 67 string MNum = this.getMNum();//获取注册码 68 for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中 69 { 70 Charcode[i] = Convert.ToChar(MNum.Substring(i - 1, 1)); 71 } 72 for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。 73 { 74 intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]); 75 } 76 string strAsciiName = "";//用于存储注册码 77 for (int j = 1; j < intNumber.Length; j++) 78 { 79 if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间 80 { 81 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 82 } 83 else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间 84 { 85 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 86 } 87 else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间 88 { 89 strAsciiName += Convert.ToChar(intNumber[j]).ToString(); 90 } 91 else//判断字符ASCII值不在以上范围内 92 { 93 if (intNumber[j] > 122)//判断字符ASCII值是否大于z 94 { 95 strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString(); 96 } 97 else 98 { 99 strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString(); 100 } 101 } 102 } 103 return strAsciiName;//返回注册码 104 } 105 } 106 }