Demon

Disponível somente no TrabalhosFeitos
  • Páginas : 5 (1075 palavras )
  • Download(s) : 0
  • Publicado : 8 de abril de 2012
Ler documento completo
Amostra do texto
I googled for sometime but i couldn't get what i want therefore i decided to write a Custom serial number generator class in C# .NET.

I try to figured out the method that return the following serial number format such as

SN = xxxx-xxxx-xxxx-xxxx-xxxx-xxxx

e.g : 85F7-1DF2-BB23-4C32-A684-C5D3
here's the one line of code :
string serialNumber =RandomSNKGenerator.GetSerialKeyAlphaNumaric(SNKeyLength.SN16);
or if you want to generate random number only :
string serialNumberNumberOnly =
RandomSNKGenerator.GetSerialKeyNumaric(SNKeyNumLength.SN12);
Simple?

if you want to produce a complicated serial number key for your product, i think using
GetSerialKeyAlphaNumaric() method would be great. If you are very simple minded GetSerialKeyNumaric() still sufficient enough. :)

Here the class :using System;
using System.Collections.Generic;
using
System.Text;

namespace SNGenerator
{
public enum
SNKeyLength
{
SN16 = 16, SN20 = 20, SN24 = 24, SN28 = 28, SN32 = 32
}
public enum SNKeyNumLength
{
SN4= 4 , SN8 = 8, SN12 =
12
}
public static class RandomSNKGenerator
{
private static string
AppendSpecifiedStr(int length, string str, char[] newKey)
{
string
newKeyStr = "";
int k = 0;
for (int i =0; i < length; i++)
{
for
(k = i; k < 4 + i; k++)
{
newKeyStr += newKey[k];
}
if (k ==
length)
{
break;
}
else
{
i = (k) - 1;
newKeyStr +=
str;
}
}
return newKeyStr;
}
///

/// Generate
standard serial key with alphanumaric format
///
///
the supported length of the serial
key
/// returns formated serial
key
public static string GetSerialKeyAlphaNumaric(SNKeyLength
keyLength)
{
Guid newguid= Guid.NewGuid();
string randomStr =
newguid.ToString("N");
string tracStr = randomStr.Substring(0,
(int)keyLength);
tracStr = tracStr.ToUpper();
char[] newKey =
tracStr.ToCharArray();
string newSerialNumber = "";
switch (keyLength
)
{
case SNKeyLength.SN16:
newSerialNumber = AppendSpecifiedStr(16,
"-", newKey);
break;
case SNKeyLength.SN20:
newSerialNumber =
AppendSpecifiedStr(20, "-", newKey);break;
case
SNKeyLength.SN24:
newSerialNumber = AppendSpecifiedStr(24, "-",
newKey);
break;
case SNKeyLength.SN28:
newSerialNumber =
AppendSpecifiedStr(28, "-", newKey);
break;
case
SNKeyLength.SN32:
newSerialNumber = AppendSpecifiedStr(32, "-",
newKey);
break;
}

return newSerialNumber;
}
///

/// Generate serial key with only numaric
///

/// the supported length of
the serial key
/// returnsformated serial
key
public static string GetSerialKeyNumaric(SNKeyNumLength
keyLength)
{
Random rn = new Random();
double sd =
Math.Round(rn.NextDouble() * Math.Pow(10, (int)keyLength) + 4);
return
sd.ToString().Substring(0,(int)keyLength);
}
}
}

Good luck!
Posted by Carso Leongat 2:54 PM 
-------------------------------------------------
24 comments:

speeddemon92 said...
Thanks a lot!!!!!

Since Iwanted some extra features I modified the code to add delimiters and the distance between them as you can tell here.

public static string GetSerialKeyAlphaNumaric(SNKeyLength keyLength, SNDelimiter delimiter, int delimiterLength)

The tab will NOT work in a single-line textbox

Here is the modified code! :)

using System;
using System.Collections.Generic;
using System.Text;

namespaceSNGenerator
{
public enum SNDelimiter
{
None =0, Tab = 1, Space = 2, Hyphen = 3, Period = 4, Slash = 5, Comma = 6
}

public enum SNKeyLength
{
SN16 = 16, SN20 = 20, SN24 = 24, SN28 = 28, SN32 = 32
}
public enum SNKeyNumLength
{
SN4= 4 , SN8 = 8, SN12 = 12
}
public static class RandomSNKGenerator
{
private static string convertDelimiter(SNDelimiter delInt)
{
String temp = "";
switch(Convert.ToInt32(delInt))
{
case 0:
temp = "";
break;
case 1:
temp = "\t";
break;
case 2:
temp = " ";
break;
case 3:
temp = "-";
break;
case 4:
temp = ".";
break;
case 5:
temp = "/";
break;
case 6:
temp = ",";
break; 
}
return temp;
}

private static string AppendSpecifiedStr(int length, int delLength, string str, char[] newKey)
{

string newKeyStr = "";
int k = 0;
for (int i = 0; i < length; i++)
{
if (delLength !=...
tracking img