C# WinForm 软件注册的实现
生活随笔
收集整理的這篇文章主要介紹了
C# WinForm 软件注册的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SoftReg類: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;
?????????}
?????}?}?
?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();
?????????}
?????}
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();
????????????????}
????????????}
????????}
????}
}
?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;
?????????}
?????}?}?
?注冊窗體:
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();
?????????}
?????}
?}
?主窗體
?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();
????????????????}
????????????}
????????}
????}
}
轉載于:https://www.cnblogs.com/junjie94wan/archive/2012/03/02/2377878.html
總結
以上是生活随笔為你收集整理的C# WinForm 软件注册的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【酒桌文化】座次、上菜、喝酒的规矩
- 下一篇: C# 三层架构