C#实现软件注册

SoftReg类:

[csharp]  view plain  copy
 print ?
  1.  using System;  
  2.  using System.Collections.Generic;  
  3.  using System.Linq;  
  4.  using System.Text;  
  5.  using System.Management;  
  6.    
  7.  namespace SoftRegister  
  8.  {  
  9.      class SoftReg  
  10.      {  
  11.    ///<summary>  
  12.    /// 获取硬盘卷标号  
  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序列号  
  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.    /// 生成机器码  
  41.    ///</summary>  
  42.    ///<returns></returns>  
  43.          public string GetMNum()   
  44.          {  
  45.              string strNum = GetCpu()+GetDiskVolumeSerialNumber();  
  46.              string strMNum = strNum.Substring(0,24);    //截取前24位作为机器码  
  47.              return strMNum;  
  48.          }  
  49.    
  50.          public int[] intCode = new int[127];    //存储密钥  
  51.          public char[] charCode = new char[25];  //存储ASCII码  
  52.          public int[] intNumber = new int[25];   //存储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.    /// 生成注册码  
  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++)   //存储机器码  
  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 = "";   //注册码  
  80.              for (int k = 1; k < intNumber.Length; k++)  //生成注册码  
  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.  }  


注册窗体:

[csharp]  view plain  copy
 print ?
  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 frmRegisterForm : Form  
  14.     {  
  15.         public frmRegisterForm()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         public static bool state = true;  //软件是否为可用状态  
  20.         SoftReg softReg = new SoftReg();  
  21.         private void btnClose_Click(object sender, EventArgs e)  
  22.         {  
  23.             if (state == true)  
  24.             {  
  25.                 this.Close();  
  26.             }  
  27.             else   
  28.             {  
  29.                 Application.Exit();  
  30.             }  
  31.         }  
  32.   
  33.         private void btnReg_Click(object sender, EventArgs e)  
  34.         {  
  35.             try  
  36.             {  
  37.                 if (txtRNum.Text == softReg.GetRNum())  
  38.                 {  
  39.                     MessageBox.Show("注册成功!重启软件后生效!""信息", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  40.                     RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software"true).CreateSubKey("wxf").CreateSubKey("wxf.INI").CreateSubKey(txtRNum.Text);  
  41.                     retkey.SetValue("UserName""Rsoft");  
  42.                     this.Close();  
  43.                 }  
  44.                 else  
  45.                 {  
  46.                     MessageBox.Show("注册码错误!""警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  47.                     txtRNum.SelectAll();  
  48.                 }  
  49.             }  
  50.             catch (Exception ex)  
  51.             {  
  52.                 throw new Exception(ex.Message);  
  53.             }  
  54.         }  
  55.   
  56.         private void frmRegisterForm_Load(object sender, EventArgs e)  
  57.         {  
  58.             this.txtMNum.Text = softReg.GetMNum();  
  59.         }  
  60.     }  
  61. }  


主窗体:

[csharp]  view plain  copy
 print ?
  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 frmMainForm : Form  
  14.     {  
  15.         public frmMainForm()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         SoftReg softReg = new SoftReg();  
  20.         private void btnClose_Click(object sender, EventArgs e)  
  21.         {  
  22.             Application.Exit();  
  23.         }  
  24.   
  25.         private void btnReg_Click(object sender, EventArgs e)  
  26.         {  
  27.             frmRegisterForm frmRegister = new frmRegisterForm();  
  28.             frmRegister.ShowDialog();  
  29.         }  
  30.   
  31.         ///<summary>  
  32.         /// 窗体加载  
  33.         ///</summary>  
  34.         ///<param name="sender"></param>  
  35.         ///<param name="e"></param>  
  36.         private void frmMainForm_Load(object sender, EventArgs e)  
  37.         {  
  38.             //判断软件是否注册  
  39.             RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE"true).CreateSubKey("wxf").CreateSubKey("wxf.INI");  
  40.             foreach (string strRNum in retkey.GetSubKeyNames())  
  41.             {  
  42.                 if (strRNum == softReg.GetRNum())  
  43.                 {  
  44.                     this.lblRegInfo.Text = "此软件已注册!";  
  45.                     this.btnReg.Enabled = false;  
  46.                     return;  
  47.                 }  
  48.             }  
  49.             this.Text = "此软件尚未注册!";  
  50.             this.btnReg.Enabled = true;  
  51.             MessageBox.Show("您现在使用的是试用版,可以免费试用30次!","信息",MessageBoxButtons.OK,MessageBoxIcon.Information);  
  52.             Int32 tLong;  
  53.             try  
  54.             {  
  55.                  tLong= (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel""UseTimes", 0);  
  56.                 MessageBox.Show("您已经使用了" + tLong + "次!""信息", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  57.             }  
  58.             catch   
  59.             {  
  60.                 MessageBox.Show("欢迎使用本软件!","信息",MessageBoxButtons.OK,MessageBoxIcon.Information);  
  61.                 Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel","UseTimes",0,RegistryValueKind.DWord);  
  62.             }  
  63.             tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel""UseTimes", 0);  
  64.             if (tLong < 30)  
  65.             {  
  66.                 int tTimes = tLong + 1;  
  67.                 Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel""UseTimes", tTimes);  
  68.             }  
  69.             else   
  70.             {  
  71.                 DialogResult result = MessageBox.Show("试用次数已到!您是否需要注册?""信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);  
  72.                 if (result == DialogResult.Yes)  
  73.                 {  
  74.                     frmRegisterForm.state = false;  
  75.                     btnReg_Click(sender, e);  
  76.                 }  
  77.                 else   
  78.                 {  
  79.                     Application.Exit();  
  80.                 }  
  81.             }  
  82.         }  
  83.     }  
  84. }  
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值