一、創建生成機器碼的類

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;//添加引用註冊表namespace DisDemo
{
    internal class SoftRegs
    {
        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(string strMNum)
        {
            SetIntCode();            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;
        }
    }
}

二、創建生成機器碼窗體

license綁定機器碼 適用於容器嗎_ci

 

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 DisDemo
{
    public partial class FormRegister : Form
    {
        public FormRegister()
        {
            InitializeComponent();
        }
        public static bool state = true;  //軟件是否為可用狀態
         SoftReg softReg = new SoftReg();        private void btnReg_Click(object sender, EventArgs e)
        {
            try
             {
                                 if (txtLicence.Text == softReg.GetRNum())
                                     {
                                         MessageBox.Show("註冊成功!重啓軟件後生效!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                         RegistryKey retkey = Registry.CurrentUser.OpenSubKey("Software", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI").CreateSubKey(txtLicence.Text);
                                         retkey.SetValue("UserName", "Rsoft");
                                         this.Close();
                                     }
                                 else
                                     {
                                         MessageBox.Show("註冊碼錯誤!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                         txtLicence.SelectAll();
                                     }
                             }
                         catch (Exception ex)
               {
                                 throw new Exception(ex.Message);
                             }
            }        private void btnClose_Click(object sender, EventArgs e)
        {
            if (state == true)
                             {
                                 this.Close();
                             }
                         else
                             {
                                 Application.Exit();
                                 Environment.Exit(0);
            }
        }        private void FormRegister_Load(object sender, EventArgs e)
        {
            this.txtHardware.Text = softReg.GetMNum();
        }
    }
}

 三、主窗體判斷是否註冊

license綁定機器碼 適用於容器嗎_ci_02

 

 

 

 

 

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 FormMain : Form
     {
        public FormMain()
        {
             InitializeComponent();
        }
  
         SoftReg softReg = new SoftReg();
  
        private void FormMain_Load(object sender, EventArgs e)
         {
            //判斷軟件是否註冊
               RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI");
            foreach (string strRNum in retkey.GetSubKeyNames())
             {
                if (strRNum == softReg.GetRNum())
                {
                     this.labRegInfo.Text = "此軟件已註冊!";
                     this.btnReg.Enabled = false;
                    return;
                 }
            }
             this.labRegInfo.Text = "此軟件尚未註冊!";
            this.btnReg.Enabled = true;
            MessageBox.Show("您現在使用的是試用版,可以免費試用30次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
             
             Int32 tLong;    //已使用次數
             try
             {
                 tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);
                 MessageBox.Show("您已經使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
             catch
             {
                MessageBox.Show("歡迎使用本軟件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0, RegistryValueKind.DWord);
            }
  
             //判斷是否可以繼續試用
            tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", 0);
             if (tLong < 30)
             {
                int tTimes = tLong + 1;
                 Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\mySoftWare", "UseTimes", tTimes);
            }
             else
             {
                 DialogResult result = MessageBox.Show("試用次數已到!您是否需要註冊?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                 if (result == DialogResult.Yes)
                 {
                     FormRegister.state = false; //設置軟件狀態為不可用
                    btnReg_Click(sender, e);    //打開註冊窗口
                 }
                else
                 {
                     Application.Exit();
                }
             }
  
         }
  
         private void btnClose_Click(object sender, EventArgs e)
         {
            Application.Exit();
        }
 
        private void btnReg_Click(object sender, EventArgs e)
         {
             FormRegister frmRegister = new FormRegister();
            frmRegister.ShowDialog();
        }
    }
 }
四、創建生成註冊碼類
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;//添加引用

namespace DisDemo
{
internal class SoftRegs
{
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(string strMNum)
{
SetIntCode();

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;
}
}
}

五、創建註冊碼窗體

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;

namespace DisDemo
{
public partial class zhuce : Form
{
SoftRegs softReg = new SoftRegs();
public zhuce()

{
InitializeComponent();
}

private void btnReg_Click(object sender, EventArgs e)
{
try
{
string strHardware = this.txtHardware.Text;
string strLicence = softReg.GetRNum(strHardware);
this.txtLicence.Text = strLicence;
}
catch (System.Exception ex)
{
MessageBox.Show("輸入的機器碼格式錯誤!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

private void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}