代码结构:
FrmShouQuan.cs 是注册界面
MainInterface.cs 是注册成功后才能访问的主界面
代码:
using System;
using System.Management;
using System.Windows.Forms;
namespace KeyManager
{
public partial class FrmShouQuan : Form
{
private string MachineInfo = string.Empty;
public FrmShouQuan()
{
InitializeComponent();
MachineInfodata();
}
/// <summary>
/// 获取CPU的参数
/// </summary>
/// <returns></returns>
public string getCpu()
{
string strCpu = null;
ManagementClass myCpu = new ManagementClass("win32_Processor");
ManagementObjectCollection myCpuConnection = myCpu.GetInstances();
foreach (ManagementObject myObject in myCpuConnection)
{
strCpu = myObject.Properties["Processorid"].Value.ToString();
break;
}
return strCpu;
}
/// <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>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MachineInfodata()
{
MachineInfo = getCpu() + GetDiskVolumeSerialNumber();//获得24位Cpu和硬盘序列号
}
public int[] intCode = new int[127];//用于存密钥
public void setIntCode()
{
for (int i = 1; i < intCode.Length; i++)
{
intCode[i] = i % 10;
}
}
public int[] intNumber = new int[25];//用于存机器码的Ascii值
public char[] Charcode = new char[25];//存储机器码字
//生成注册码
private void button2_Click(object sender, EventArgs e)
{
txtRegistText.Text = GetResistText(MachineInfo);
}
/// <summary>
/// 根据机器码获取注册码
/// </summary>
/// <param name="machineText"></param>
/// <returns></returns>
private string GetResistText(string machineText)
{
//把机器码存入数组中
setIntCode();//初始化127位数组
for (int i = 1; i < Charcode.Length; i++)//把机器码存入数组中
{
Charcode[i] = Convert.ToChar(machineText.Substring(i - 1, 1));
}
for (int j = 1; j < intNumber.Length; j++)//把字符的ASCII值存入一个整数组中。
{
intNumber[j] = intCode[Convert.ToInt32(Charcode[j])] + Convert.ToInt32(Charcode[j]);
}
string strAsciiName = null;//用于存储机器码
for (int j = 1; j < intNumber.Length; j++)
{
if (intNumber[j] >= 48 && intNumber[j] <= 57)//判断字符ASCII值是否0-9之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else if (intNumber[j] >= 65 && intNumber[j] <= 90)//判断字符ASCII值是否A-Z之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else if (intNumber[j] >= 97 && intNumber[j] <= 122)//判断字符ASCII值是否a-z之间
{
strAsciiName += Convert.ToChar(intNumber[j]).ToString();
}
else//判断字符ASCII值不在以上范围内
{
if (intNumber[j] > 122)//判断字符ASCII值是否大于z
{ strAsciiName += Convert.ToChar(intNumber[j] - 10).ToString(); }
else
{
strAsciiName += Convert.ToChar(intNumber[j] - 9).ToString();
}
}
}
return strAsciiName;
}
private void btnRegist_Click(object sender, EventArgs e)
{
if (txtRegistText.Text != "")
{
//这里是根据机器码进行判断,您也可以换成您需要的参数进行判断
if (textBox1.Text.TrimEnd().Equals(txtRegistText.Text.TrimEnd()))
{
//这里需要将注册码保存到注册表中
LicenseManager.WTRegedit("MachineCode", txtRegistText.Text);
if (LicenseManager.IsRegeditExit("MachineCode"))
{
MessageBox.Show("注册成功!");
return;
}
else
{
MessageBox.Show("注册失败");
}
}
else
{
MessageBox.Show("注册码输入错误");
}
}
else { MessageBox.Show("请生成注册码", "注册提示"); }
}
/// <summary>
/// 验证是否已经激活软件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
if (LicenseManager.IsRegeditExit("MachineCode"))
{
MessageBox.Show("许可证有效!");
return;
}
else
{
MessageBox.Show("许可证无效");
}
}
}
}
namespace KeyManager
{
partial class FrmShouQuan
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.txtRegistText = new System.Windows.Forms.TextBox();
this.button4 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button2
//
this.button2.Location = new System.Drawing.Point(121, 218);
this.button2.Margin = new System.Windows.Forms.Padding(2);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(74, 26);
this.button2.TabIndex = 2;
this.button2.Text = "生成注册码";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(214, 218);
this.button3.Margin = new System.Windows.Forms.Padding(2);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(56, 26);
this.button3.TabIndex = 2;
this.button3.Text = "注册";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.btnRegist_Click);
//
// txtRegistText
//
this.txtRegistText.Location = new System.Drawing.Point(176, 53);
this.txtRegistText.Margin = new System.Windows.Forms.Padding(2);
this.txtRegistText.Name = "txtRegistText";
this.txtRegistText.Size = new System.Drawing.Size(210, 21);
this.txtRegistText.TabIndex = 1;
//
// button4
//
this.button4.Location = new System.Drawing.Point(291, 218);
this.button4.Margin = new System.Windows.Forms.Padding(2);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 26);
this.button4.TabIndex = 2;
this.button4.Text = "检查是否注册";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(119, 56);
this.label2.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 12);
this.label2.TabIndex = 3;
this.label2.Text = "授权码:";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(178, 114);
this.textBox2.Margin = new System.Windows.Forms.Padding(2);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(210, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Visible = false;
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(74, 117);
this.label4.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(101, 12);
this.label4.TabIndex = 3;
this.label4.Text = "请输入起始日期:";
this.label4.Visible = false;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(178, 144);
this.textBox3.Margin = new System.Windows.Forms.Padding(2);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(210, 21);
this.textBox3.TabIndex = 1;
this.textBox3.Visible = false;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(74, 146);
this.label5.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(101, 12);
this.label5.TabIndex = 3;
this.label5.Text = "请输入有效天数:";
this.label5.Visible = false;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(178, 85);
this.textBox1.Margin = new System.Windows.Forms.Padding(2);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(210, 21);
this.textBox1.TabIndex = 1;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(86, 86);
this.label3.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(89, 12);
this.label3.TabIndex = 3;
this.label3.Text = "请输入授权码:";
//
// FrmShouQuan
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(516, 271);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.txtRegistText);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Margin = new System.Windows.Forms.Padding(2);
this.Name = "FrmShouQuan";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FrmShouQuan";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.TextBox txtRegistText;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label3;
}
}
using Microsoft.Win32;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace KeyManager
{
public class LicenseManager
{
public static void WTRegedit(string name, string tovalue)
{
RegistryKey hklm = Registry.CurrentUser;
RegistryKey software = hklm.OpenSubKey("SOFTWARE", true);
RegistryKey aimdir = software.CreateSubKey("MyApp");
aimdir.SetValue(name, tovalue);
}
public static bool IsRegeditExit(string name)
{
bool _exit = false;
RegistryKey hkml = Registry.CurrentUser;
RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
RegistryKey aimdir = software.OpenSubKey("MyApp", true);
if (aimdir != null)
{
object oVersion = aimdir.GetValue("MachineCode");
if (null != oVersion)
{
_exit = true;
return _exit;
}
}
return _exit;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace KeyManager
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
if (LicenseManager.IsRegeditExit("MachineCode"))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainInterface());
return;
}
else
{
MessageBox.Show("许可证无效,请进行注册!");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FrmShouQuan());
}
}
}
}