• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#(WinForm)实现软件注册

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

C#(WinForm)实现软件注册

View Code
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Management;
 
 namespace SoftRegister
 {
     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 myCpuCollection = myCpu.GetInstances();
             foreach (ManagementObject myObject in myCpuCollection)
             {
                 strCpu = myObject.Properties["Processorid"].Value.ToString();
             }
             return strCpu;
         }
 
         ///<summary>
 /// 生成机器码
 ///</summary>
 ///<returns></returns>
         public string GetMNum()
         {
             string strNum = GetCpu()+GetDiskVolumeSerialNumber();
             string strMNum = strNum.Substring(0,24);    //截取前24位作为机器码
             return strMNum;
         }
 
         public int[] intCode = new int[127];    //存储密钥
         public char[] charCode = new char[25];  //存储ASCII码
         public int[] intNumber = new int[25];   //存储ASCII码值
 
 //初始化密钥
         public void SetIntCode()
         {
             for (int i = 1; i < intCode.Length; i++)
             {
                 intCode[i] = i % 9;
             }
         }
 
         ///<summary>
 /// 生成注册码
 ///</summary>
 ///<returns></returns>
         public string GetRNum()
         {
             SetIntCode();
             string strMNum = GetMNum();
             for (int i = 1; i < charCode.Length; i++)   //存储机器码
             {
                 charCode[i] =Convert.ToChar(strMNum.Substring(i - 1, 1));
             }
             for (int j = 1; j < intNumber.Length; j++)  //改变ASCII码值
             {
                 intNumber[j] = Convert.ToInt32(charCode[j]) + intCode[Convert.ToInt32(charCode[j])];
             }
             string strAsciiName = "";   //注册码
             for (int k = 1; k < intNumber.Length; k++)  //生成注册码
             {
                
                 if ((intNumber[k] >= 48 && intNumber[k] <= 57) || (intNumber[k] >= 65 && intNumber[k]
                     <= 90) || (intNumber[k] >= 97 && intNumber[k] <= 122))  //判断如果在0-9、A-Z、a-z之间
                 {
                     strAsciiName += Convert.ToChar(intNumber[k]).ToString();
                 }
                 else if (intNumber[k] > 122)  //判断如果大于z
                 {
                     strAsciiName += Convert.ToChar(intNumber[k] - 10).ToString();
                 }
                 else
                 {
                     strAsciiName+=Convert.ToChar(intNumber[k]-9).ToString();
                 }
             }
             return strAsciiName;
         }
     }
 }

 

View Code
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Windows.Forms;
 using Microsoft.Win32;
 
 namespace SoftRegister
 {
     public partial class frmRegisterForm : Form
     {
         public frmRegisterForm()
         {
             InitializeComponent();
         }
         public static bool state = true;  //软件是否为可用状态
         SoftReg softReg = new SoftReg();
         private void btnClose_Click(object sender, EventArgs e)
         {
             if (state == true)
             {
                 this.Close();
             }
             else
             {
                 Application.Exit();
             }
         }
 
         private void btnReg_Click(object sender, EventArgs e)
         {
             try
             {
                 if (txtRNum.Text == softReg.GetRNum())
                 {
                     MessageBox.Show("注册成功!重启软件后生效!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("wxf").CreateSubKey("wxf.INI").CreateSubKey(txtRNum.Text);
                     retkey.SetValue("UserName", "Rsoft");
                     this.Close();
                 }
                 else
                 {
                     MessageBox.Show("注册码错误!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                     txtRNum.SelectAll();
                 }
             }
             catch (Exception ex)
             {
                 throw new Exception(ex.Message);
             }
         }
 
         private void frmRegisterForm_Load(object sender, EventArgs e)
         {
             this.txtMNum.Text = softReg.GetMNum();
         }
     }
 }

 

View Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace SoftRegister
{
    public partial class frmMainForm : Form
    {
        public frmMainForm()
        {
            InitializeComponent();
        }
        SoftReg softReg = new SoftReg();
        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnReg_Click(object sender, EventArgs e)
        {
            frmRegisterForm frmRegister = new frmRegisterForm();
            frmRegister.ShowDialog();
        }

        ///<summary>
        /// 窗体加载
        ///</summary>
        ///<param name="sender"></param>
        ///<param name="e"></param>
        private void frmMainForm_Load(object sender, EventArgs e)
        {
            //判断软件是否注册
            RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("wxf").CreateSubKey("wxf.INI");
            foreach (string strRNum in retkey.GetSubKeyNames())
            {
                if (strRNum == softReg.GetRNum())
                {
                    this.lblRegInfo.Text = "此软件已注册!";
                    this.btnReg.Enabled = false;
                    return;
                }
            }
            this.Text = "此软件尚未注册!";
            this.btnReg.Enabled = true;
            MessageBox.Show("您现在使用的是试用版,可以免费试用30次!","信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
            Int32 tLong;
            try
            {
                 tLong= (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0);
                MessageBox.Show("您已经使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("欢迎使用本软件!","信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
                Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel","UseTimes",0,RegistryValueKind.DWord);
            }
            tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", 0);
            if (tLong < 30)
            {
                int tTimes = tLong + 1;
                Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Angel", "UseTimes", tTimes);
            }
            else
            {
                DialogResult result = MessageBox.Show("试用次数已到!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (result == DialogResult.Yes)
                {
                    frmRegisterForm.state = false;
                    btnReg_Click(sender, e);
                }
                else
                {
                    Application.Exit();
                }
            }
        }
    }
}

http://www.cnblogs.com/ynbt/
关于作者:专注于.Net、WCF和移动互联网开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过[email protected]联系我,非常感谢。 。
 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap