WINDOWS FORMS: HOW TO CREATE A LICENSE KEY IN C#

2407

Create License key, Serial Key Maker, Generate Product Key, Validate a software License Key in C#

Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project “LicenseKey” and then click OK

 

Step 2: Download FoxLearn.License.dll, then add a reference to the FoxLearn.License.dll

Step 3: Design your form as below

Name your main form: Form1

Name your generate license form: frmGenerate

Name  about form: frmAbout

 

Add code to handle Form1

 

namespace LicenseKey
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            using(frmGenerate frm = new frmGenerate())
            {
                frm.ShowDialog();
            }
        }
        private void btnRegistration_Click(object sender, EventArgs e)
        {
            using (frmRegistration frm = new frmRegistration())
            {
                frm.ShowDialog();
            }
        }
        private void btnAbout_Click(object sender, EventArgs e)
        {
            using (frmAbout frm = new frmAbout())
            {
                frm.ShowDialog();
            }
        }
    }
}
https://www.youtube.com/watch?v=ctP910qdHhA&feature=youtu.be

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

 

 

If you need the code Leave your email in comment I send you the code in Email.