一个序列号生成器

时间:2024-03-01 22:12:01
public string CreateSerialNumber(int strLength)
{
    if (strLength < 1)
        strLength = 4;

    string str = string.Empty;
    string codeStr = string.Empty;
    string guidStr = Guid.NewGuid().ToString().Replace("-", "");
    int guidStrLen = guidStr.Length;
    Random rnd = new Random(int.Parse(DateTime.Now.ToString("MMddHHmmsss")));

    for (int i = 0; i < strLength; i++)
    {
        int index = rnd.Next(0, guidStrLen - 1);
        str += guidStr.Substring(index, 1);
    }

    return str.ToUpper();
}