My Soltion is:
private static Random rd = new Random((int)DateTime.Now.Ticks);
private void btn_License_Click(object sender, EventArgs e)
{
string str1 = GeneratePassword(5, 0);
string str2 = GeneratePassword(5, 0);
string str3 = GeneratePassword(5, 0);
string str4 = GeneratePassword(5, 0);
string str5 = GeneratePassword(5, 0);
textBox1.Text = str1 + "-" + str2 + "-" + str3 + "-" + str4 + "-" + str5;
}
public static string GeneratePassword(int Lenght, int NonAlphaNumericChars)
{
string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string allowedNonAlphaNum = "!@#$%^&*()_-+=[{]};:<>|./?";
if (NonAlphaNumericChars > Lenght || Lenght <= 0 || NonAlphaNumericChars < 0)
throw new ArgumentOutOfRangeException();
char[] pass = new char[Lenght];
int[] pos = new int[Lenght];
int i = 0, j = 0, temp = 0;
bool flag = false;
while (i < Lenght - 1)
{
j = 0;
flag = false;
temp = rd.Next(0, Lenght);
for (j = 0; j < Lenght; j++)
if (temp == pos[j])
{
flag = true;
j = Lenght;
}
if (!flag)
{
pos[i] = temp;
i++;
}
}
for (i = 0; i < Lenght - NonAlphaNumericChars; i++)
pass[i] = allowedChars[rd.Next(0, allowedChars.Length)];
for (i = Lenght - NonAlphaNumericChars; i < Lenght; i++)
pass[i] = allowedNonAlphaNum[rd.Next(0, allowedNonAlphaNum.Length)];
char[] sorted = new char[Lenght];
for (i = 0; i < Lenght; i++)
sorted[i] = pass[pos[i]];
string Pass = new String(sorted);
return Pass;
}
Result is:Y4CDQ-GAIW5-VIWE1-0EFN6-C5Z7H