C#实现注册码注册机制效果详解
芝麻粒儿 人气:0实践过程
效果
代码
public partial class frmMain : Form { public frmMain() { InitializeComponent(); } SoftReg softreg = new SoftReg();//实例化公共类对象 private void frmMain_Load(object sender, EventArgs e) { RegistryKey retkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software", true).CreateSubKey("mrwxk").CreateSubKey("mrwxk.ini"); foreach (string strRNum in retkey.GetSubKeyNames())//判断是否注册 { if (strRNum == softreg.getRNum()) { this.Text = "主窗体(已注册)"; button1.Enabled = false; return; } } this.Text = "主窗体(未注册)"; button1.Enabled = true; MessageBox.Show("您现在使用的是试用版,该软件可以免费试用30次!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Int32 tLong; try { tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\tryTimes", "UseTimes", 0); MessageBox.Show("感谢您已使用了" + tLong + "次", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\tryTimes", "UseTimes", 0, RegistryValueKind.DWord); MessageBox.Show("欢迎新用户使用本软件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\tryTimes", "UseTimes", 0); if (tLong < 30) { int Times = tLong + 1; Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\tryTimes", "UseTimes", Times); } else { MessageBox.Show("试用次数已到", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); Application.Exit(); } } private void button1_Click(object sender, EventArgs e) { frmRegister frmregister = new frmRegister(); frmregister.Show(); this.Hide(); } private void frmMain_FormClosed(object sender, FormClosedEventArgs e) { Application.Exit(); } }
public partial class frmRegister : Form { public frmRegister() { InitializeComponent(); } SoftReg softreg = new SoftReg();//实例化公共类对象 private void frmRegister_Load(object sender, EventArgs e) { textBox1.Text = softreg.getMNum(); } private void button1_Click(object sender, EventArgs e) { if (textBox2.Text == "") { MessageBox.Show("注册码输入不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { if (textBox2.Text.Equals(softreg.getRNum())) { RegistryKey retkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("software", true).CreateSubKey("mrwxk").CreateSubKey("mrwxk.ini").CreateSubKey(textBox2.Text); retkey.SetValue("UserName", "mrsoft"); MessageBox.Show("注册成功,程序需要重新加载!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Hide(); frmMain frmmain = new frmMain(); frmmain.Show(); } else { MessageBox.Show("注册码输入错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void button2_Click(object sender, EventArgs e) { this.Hide(); frmMain frmmain = new frmMain(); frmmain.Show(); } }
public partial class frmRMachine : Form { public frmRMachine() { InitializeComponent(); } SoftReg softreg = new SoftReg();//实例化公共类对象 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "") { MessageBox.Show("机器码输入不能为空!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { textBox2.Text = softreg.getRNum(); } } private void button2_Click(object sender, EventArgs e) { this.Close(); } }
public class SoftReg { // 取得设备硬盘的卷标号 public string GetDiskVolumeSerialNumber() { ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"d:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } //获得CPU的序列号 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; } //生成机器码 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; } } //生成注册码 public string getRNum() { setIntCode();//初始化127位数组 for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中 { Charcode[i] = Convert.ToChar(this.getMNum().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; } }
加载全部内容