The class :
class KeyGenerator
{
public KeyGenerator()
{
chars = new List<char>();
generated = new List<string>();
for (int i = 0; i < 10; i++)
chars.Add((char)('0' + i));
for (char c = 'A'; c <= 'Z'; c++)
chars.Add(c);
random = new Random();
}
private List<char> chars;
private List<string> generated;
Random random;
private List<string> keys;
public string GetNext(int len)
{
StringBuilder result = new StringBuilder();
bool duplicate = true;
while(duplicate)
{
result.Clear();
for (int i = 0; i < len; i++)
{
int index = random.Next(chars.Count);
result.Append(chars[index]);
}
if (!generated.Contains(result.ToString()))
duplicate = false;
}
return result.ToString();
}
}
Its usage is :
KeyGenerator keyGenerator = new KeyGenerator();
for (int i = 0; i < 5; i++)
Console.WriteLine(keyGenerator.GetNext(16));
Hope it helps.