c# 自定义位数生成激活码

时间:2024-10-13 20:34:14

Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
private string CreateAndCheckCode(Random random, string code, int length) // code 激活码前缀
{

char[] Pattern = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string result = code;
int n = Pattern.Length;
for (int i = 0; i < length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
if (数据库中不存在)
{
return result;
}
else
{
return CreateAndCheckCode(random, code, length);
}
}