C#学习笔记——软件注册与注册机
生活随笔
收集整理的這篇文章主要介紹了
C#学习笔记——软件注册与注册机
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
(一)軟件的實(shí)現(xiàn):
SoftReg類:
1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: using System.Management; //需要引用System.Management.dll 6:? 7: namespace SoftRegister 8: { 9: class SoftReg 10: { 11: ///<summary> 12: /// 獲取硬盤卷標(biāo)號(hào) 13: ///</summary> 14: ///<returns></returns> 15: public string GetDiskVolumeSerialNumber() 16: { 17: ManagementClass mc = new ManagementClass("win32_NetworkAdapterConfiguration"); 18: ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); 19: disk.Get(); 20: return disk.GetPropertyValue("VolumeSerialNumber").ToString(); 21: } 22:? 23: ///<summary> 24: /// 獲取CPU序列號(hào) 25: ///</summary> 26: ///<returns></returns> 27: public string GetCpu() 28: { 29: string strCpu = null; 30: ManagementClass myCpu = new ManagementClass("win32_Processor"); 31: ManagementObjectCollection myCpuCollection = myCpu.GetInstances(); 32: foreach (ManagementObject myObject in myCpuCollection) 33: { 34: strCpu = myObject.Properties["Processorid"].Value.ToString(); 35: } 36: return strCpu; 37: } 38:? 39: ///<summary> 40: /// 生成機(jī)器碼 41: ///</summary> 42: ///<returns></returns> 43: public string GetMNum() 44: { 45: string strNum = GetCpu() + GetDiskVolumeSerialNumber(); 46: string strMNum = strNum.Substring(0, 24); //截取前24位作為機(jī)器碼 47: return strMNum; 48: } 49:? 50: public int[] intCode = new int[127]; //存儲(chǔ)密鑰 51: public char[] charCode = new char[25]; //存儲(chǔ)ASCII碼 52: public int[] intNumber = new int[25]; //存儲(chǔ)ASCII碼值 53:? 54: //初始化密鑰 55: public void SetIntCode() 56: { 57: for (int i = 1; i < intCode.Length; i++) 58: { 59: intCode[i] = i % 9; 60: } 61: } 62:? 63: ///<summary> 64: /// 生成注冊(cè)碼 65: ///</summary> 66: ///<returns></returns> 67: public string GetRNum() 68: { 69: SetIntCode(); 70: string strMNum = GetMNum(); 71: for (int i = 1; i < charCode.Length; i++) //存儲(chǔ)機(jī)器碼 72: { 73: charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1)); 74: } 75: for (int j = 1; j < intNumber.Length; j++) //改變ASCII碼值 76: { 77: intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])]; 78: } 79: string strAsciiName = ""; //注冊(cè)碼 80: for (int k = 1; k < intNumber.Length; k++) //生成注冊(cè)碼 81: { 82:? 83: if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k] 84: <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122)) //判斷如果在0-9、A-Z、a-z之間 85: { 86: strAsciiName += Convert.ToChar(intNumber[k]).ToString(); 87: } 88: else if (intNumber[k] > 122) //判斷如果大于z 89: { 90: strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString(); 91: } 92: else 93: { 94: strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString(); 95: } 96: } 97: return strAsciiName; 98: } 99: } 100: } 主窗體: 1: using System; 2: using System.Collections.Generic; 3: using System.ComponentModel; 4: using System.Data; 5: using System.Drawing; 6: using System.Linq; 7: using System.Text; 8: using System.Windows.Forms; 9: using Microsoft.Win32; 10:? 11: namespace SoftRegister 12: { 13: public partial class FormMain : Form 14: { 15: public FormMain() 16: { 17: InitializeComponent(); 18: } 19:? 20: SoftReg softReg = new SoftReg(); 21:? 22: private void FormMain_Load(object sender, EventArgs e) 23: { 24: //判斷軟件是否注冊(cè) 25: RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI"); 26: foreach (string strRNum in retkey.GetSubKeyNames()) 27: { 28: if (strRNum == softReg.GetRNum()) 29: { 30: this.labRegInfo.Text = "此軟件已注冊(cè)!"; 31: this.btnReg.Enabled = false; 32: return; 33: } 34: } 35: this.labRegInfo.Text = "此軟件尚未注冊(cè)!"; 36: this.btnReg.Enabled = true; 37: MessageBox.Show("您現(xiàn)在使用的是試用版,可以免費(fèi)試用30次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); 38: 39: Int32 tLong; //已使用次數(shù) 40: try 41: { 42: tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0); 43: MessageBox.Show("您已經(jīng)使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); 44: } 45: catch 46: { 47: MessageBox.Show("歡迎使用本軟件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); 48: Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0, RegistryValueKind.DWord); 49: } 50:? 51: //判斷是否可以繼續(xù)試用 52: tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0); 53: if (tLong < 30) 54: { 55: int tTimes = tLong + 1; 56: Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", tTimes); 57: } 58: else 59: { 60: DialogResult result = MessageBox.Show("試用次數(shù)已到!您是否需要注冊(cè)?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information); 61: if (result == DialogResult.Yes) 62: { 63: FormRegister.state = false; //設(shè)置軟件狀態(tài)為不可用 64: btnReg_Click(sender, e); //打開注冊(cè)窗口 65: } 66: else 67: { 68: Application.Exit(); 69: } 70: } 71:? 72: } 73:? 74: private void btnClose_Click(object sender, EventArgs e) 75: { 76: Application.Exit(); 77: } 78:? 79: private void btnReg_Click(object sender, EventArgs e) 80: { 81: FormRegister frmRegister = new FormRegister(); 82: frmRegister.ShowDialog(); 83: } 84: } 85: }注冊(cè)窗體:
1: using System; 2: using System.Collections.Generic; 3: using System.ComponentModel; 4: using System.Data; 5: using System.Drawing; 6: using System.Linq; 7: using System.Text; 8: using System.Windows.Forms; 9: using Microsoft.Win32; 10:? 11: namespace SoftRegister 12: { 13: public partial class FormRegister : Form 14: { 15: public FormRegister() 16: { 17: InitializeComponent(); 18: } 19:? 20: public static bool state = true; //軟件是否為可用狀態(tài) 21: SoftReg softReg = new SoftReg(); 22:? 23: private void btnReg_Click(object sender, EventArgs e) 24: { 25: try 26: { 27: if (txtLicence.Text == softReg.GetRNum()) 28: { 29: MessageBox.Show("注冊(cè)成功!重啟軟件后生效!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information); 30: RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI").CreateSubKey(txtLicence.Text); 31: retkey.SetValue("UserName", "Rsoft"); 32: this.Close(); 33: } 34: else 35: { 36: MessageBox.Show("注冊(cè)碼錯(cuò)誤!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); 37: txtLicence.SelectAll(); 38: } 39: } 40: catch (Exception ex) 41: { 42: throw new Exception(ex.Message); 43: } 44: } 45:? 46: private void btnClose_Click(object sender, EventArgs e) 47: { 48: if (state == true) 49: { 50: this.Close(); 51: } 52: else 53: { 54: Application.Exit(); 55: } 56: } 57:? 58: private void FormRegister_Load(object sender, EventArgs e) 59: { 60: this.txtHardware.Text = softReg.GetMNum(); 61: } 62: } 63: } (二)注冊(cè)機(jī)的實(shí)現(xiàn): SoftReg類: 1: using System; 2: using System.Collections.Generic; 3: using System.Linq; 4: using System.Text; 5: using System.Management; 6:? 7: namespace SoftwarePassport 8: { 9: class SoftReg 10: { 11: public int[] intCode = new int[127]; //存儲(chǔ)密鑰 12: public char[] charCode = new char[25]; //存儲(chǔ)ASCII碼 13: public int[] intNumber = new int[25]; //存儲(chǔ)ASCII碼值 14:? 15: //初始化密鑰 16: public void SetIntCode() 17: { 18: for (int i = 1; i < intCode.Length; i++) 19: { 20: intCode[i] = i % 9; 21: } 22: } 23:? 24: ///<summary> 25: /// 生成注冊(cè)碼 26: ///</summary> 27: ///<returns></returns> 28: public string GetRNum(string strMNum) 29: { 30: SetIntCode(); 31: 32: for (int i = 1; i < charCode.Length; i++) //存儲(chǔ)機(jī)器碼 33: { 34: charCode[i] = Convert.ToChar(strMNum.Substring(i - 1, 1)); 35: } 36: for (int j = 1; j < intNumber.Length; j++) //改變ASCII碼值 37: { 38: intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])]; 39: } 40: string strAsciiName = ""; //注冊(cè)碼 41: for (int k = 1; k < intNumber.Length; k++) //生成注冊(cè)碼 42: { 43:? 44: if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k] 45: <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122)) //判斷如果在0-9、A-Z、a-z之間 46: { 47: strAsciiName += Convert.ToChar(intNumber[k]).ToString(); 48: } 49: else if (intNumber[k] > 122) //判斷如果大于z 50: { 51: strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString(); 52: } 53: else 54: { 55: strAsciiName += Convert.ToChar(intNumber[k] - 9).ToString(); 56: } 57: } 58: return strAsciiName; 59: } 60: } 61: } 主窗體: 1: using System; 2: using System.Collections.Generic; 3: using System.ComponentModel; 4: using System.Data; 5: using System.Drawing; 6: using System.Linq; 7: using System.Text; 8: using System.Windows.Forms; 9:? 10: namespace SoftwarePassport 11: { 12: public partial class FormMain : Form 13: { 14: public FormMain() 15: { 16: InitializeComponent(); 17: } 18:? 19: SoftReg softReg = new SoftReg(); 20:? 21: private void btnCreate_Click(object sender, EventArgs e) 22: { 23: try 24: { 25: string strHardware = this.txtHardware.Text; 26: string strLicence = softReg.GetRNum(strHardware); 27: this.txtLicence.Text = strLicence; 28: } 29: catch (System.Exception ex) 30: { 31: MessageBox.Show("輸入的機(jī)器碼格式錯(cuò)誤!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); 32: } 33: } 34:? 35: private void btnExit_Click(object sender, EventArgs e) 36: { 37: Application.Exit(); 38: } 39: } 40: }轉(zhuǎn)載于:https://www.cnblogs.com/hanzhaoxin/archive/2013/01/04/2844191.html
總結(jié)
以上是生活随笔為你收集整理的C#学习笔记——软件注册与注册机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (翻译).NET应用架构
- 下一篇: c# char unsigned_dll