Generating serial numbers and keys in C# and VB.NET

Using serial numbers is the most common way to unlock applications in the market today. Microsoft made that part of our life (us developers and isv) easier using serial keys all over their product lines. This has made final users and customer familiar with the term and how the should use them to activate their applications and that’s why we use them in our .net licensing product LicenseSpot.

One aspect to keep in mind about generating serial numbers is to keep them unique. In the function below we’re using GUIDs to generate serial numbers as we know for sure that they’re unique.

The function basically takes a guid string and just take the desired length for the key. It returns a serial number with the format: XXXX-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX.

public  string GetSerialNumber()
{
	Guid serialGuid = Guid.NewGuid();
	string uniqueSerial = serialGuid.ToString("N");

	string uniqueSerialLength = uniqueSerial.Substring(0, 28).ToUpper();

	char[] serialArray = uniqueSerialLength.ToCharArray();
	string finalSerialNumber = "";

	int j= 0;
	for (int i = 0; i < 28; i++)
	{
		for (j = i; j < 4 + i; j++)
		{
			finalSerialNumber += serialArray[j];
		}
		if (j == 28)
		{
			break;
		}
		else
		{
			i = (j) - 1;
			finalSerialNumber += "-";
		}
	}

	return finalSerialNumber;
}

Below the VB.NET code:

Public Function GetSerialNumber() As String
	Dim serialGuid As Guid = Guid.NewGuid()
	Dim uniqueSerial As String = serialGuid.ToString("N")
	Dim uniqueSerialLength As String = uniqueSerial.Substring(0, 28).ToUpper()

	Dim serialArray As Char() = uniqueSerialLength.ToCharArray()
	Dim finalSerialNumber As String = ""

	Dim j As Integer = 0
	For i As Integer = 0 To 27
		For j = i To 4 + (i - 1)
			finalSerialNumber += serialArray(j)
		Next
		If j = 28 Then
			Exit For
		Else
			i = (j) - 1
			finalSerialNumber += "-"
		End If
	Next


	Return finalSerialNumber
End Function
6 comments on “Generating serial numbers and keys in C# and VB.NET
  1. I’m a beginner in .net. I don’t understand that how do I use the and Where to place the code of “Generating serial numbers and keys in C# and VB.NET” provided in your page?
    I use visual studio, please help me..

    • Hi,

      This code basically generates a string that contains the serial number and you can include it in any class in your code. This would also go on the project that generates the serial number and sends it to the customer.

  2. How could I validate this serial number generated, so that when a user enters the installation I see that is a serial number generated by me?

    Tks

    • Hi Eric,

      You will need to keep track of your serial numbers somewhere. The best scenario will be in a database where you can the validated from app using a web service. This is where an application like LicenseSpot might help.

  3. Hello Jose,
    Thanks for above information. I don’t understand that how do I use generated product key to my application at the time of .exe installation.

    • Hi Rash,

      This code will only generate the serial number. If you want to protect your .exe file from a licensing point of view then you’ll need to implement this. Here you can find information on how to do it yourself: http://www.dotnetlicensing.net

      If you want a solution where everything is done and you just need a couple lines of code, then have a look to http://www.licensespot.com

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>