http://www.cnblogs.com/ynbt/archive/2011/11/02/2233470.html
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 }
注册窗体:
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 }
主窗体:
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(); } } } } }