Windows Forms: How to Create a License Key in C# Part 2
By Tan Lee Published on May 22, 2017 30.47K
Create License key, Serial Key Maker, Generate Product Key, Validate a software License Key in C#
Add code to handle frmGenerate form
namespace LicenseKey { public partial class frmGenerate : Form { public frmGenerate() { InitializeComponent(); } private void btnGenerate_Click(object sender, EventArgs e) { KeyManager km = new KeyManager(txtProductID.Text); KeyValuesClass kv; string productKey = string.Empty; if (cboLicenseType.SelectedIndex == 0) { kv = new KeyValuesClass() { Type = LicenseType.FULL, Header = Convert.ToByte(9), Footer = Convert.ToByte(6), ProductCode = (byte)ProductCode,//As order of your software Edition = Edition.ENTERPRISE, Version = 1 }; if (!km.GenerateKey(kv, ref productKey))//Generate full license key txtProductKey.Text = "ERROR"; } else { kv = new KeyValuesClass() { Type = LicenseType.TRIAL, Header = Convert.ToByte(9), Footer = Convert.ToByte(6), ProductCode = (byte)ProductCode, Edition = Edition.ENTERPRISE, Version = 1, Expiration = DateTime.Now.Date.AddDays(Convert.ToInt32(txtExperience.Text)) }; if (!km.GenerateKey(kv, ref productKey))//Generate trial license key txtProductKey.Text = "ERROR"; } txtProductKey.Text = productKey; } const int ProductCode = 1; private void frmGenerate_Load(object sender, EventArgs e) { cboLicenseType.SelectedIndex = 0; //Get computer id, it's unique for each computer txtProductID.Text = ComputerInfo.GetComputerId(); } } }
Add code to handle frmRegistration form
namespace LicenseKey { public partial class frmRegistration : Form { public frmRegistration() { InitializeComponent(); } const int ProductCode = 1; private void btnOK_Click(object sender, EventArgs e) { KeyManager km = new KeyManager(txtProductID.Text); string productKey = txtProductKey.Text; //Check valid if(km.ValidKey(ref productKey)) { KeyValuesClass kv = new KeyValuesClass(); //Decrypt license key if (km.DisassembleKey(productKey, ref kv)) { LicenseInfo lic = new LicenseInfo(); lic.ProductKey = productKey; lic.FullName = "FoxLearn"; if (kv.Type == LicenseType.TRIAL) { lic.Day = kv.Expiration.Day; lic.Month = kv.Expiration.Month; lic.Year = kv.Expiration.Year; } //Save license key to file km.SaveSuretyFile(string.Format(@"{0}\Key.lic", Application.StartupPath), lic); MessageBox.Show("You have been successfully registered.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); } } else MessageBox.Show("Your product key is invalid.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void frmRegistration_Load(object sender, EventArgs e) { txtProductID.Text = ComputerInfo.GetComputerId(); } } }
Add code to handle frmAbout form
namespace LicenseKey { public partial class frmAbout : Form { public frmAbout() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { this.Close(); } const int ProductCode = 1; private void frmAbout_Load(object sender, EventArgs e) { lblProductID.Text = ComputerInfo.GetComputerId(); KeyManager km = new KeyManager(lblProductID.Text); LicenseInfo lic = new LicenseInfo(); //Get license information from license file int value = km.LoadSuretyFile(string.Format(@"{0}\Key.lic", Application.StartupPath), ref lic); string productKey = lic.ProductKey; //Check valid if(km.ValidKey(ref productKey)) { KeyValuesClass kv = new KeyValuesClass(); //Decrypt license key if(km.DisassembleKey(productKey, ref kv)) { lblProductName.Text = "FoxLearn"; lblProductKey.Text = productKey; if (kv.Type == LicenseType.TRIAL) lblLicenseType.Text = string.Format("{0} days", (kv.Expiration - DateTime.Now.Date).Days); else lblLicenseType.Text = "Full"; } } } } }
VIDEO TUTORIALS
Categories
Popular Posts
10 Common Mistakes ASP.NET Developers Should Avoid
Dec 16, 2024
Freedash bootstrap lite
Nov 13, 2024
Simple Responsive Login Page
Nov 11, 2024
Material Lite Admin Template
Nov 14, 2024