c# 生成随即字符函数

singapore uncle 2023-09-22 00:21:40 阅读 0评论
调用格式  GetRandomString(6, false, true, false, false, "")

 public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
        {
            byte[] array = new byte[4];
            new RNGCryptoServiceProvider().GetBytes(array);
            Random random = new Random(BitConverter.ToInt32(array, 0));
            string text = null;
            string text2 = custom;
            if (useNum)
            {
                text2 += "0123456789";
            }
            if (useLow)
            {
                text2 += "abcdefghijklmnopqrstuvwxyz";
            }
            if (useUpp)
            {
                text2 += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            }
            if (useSpe)
            {
                text2 += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
            }
            for (int i = 0; i < length; i++)
            {
                text += text2.Substring(random.Next(0, text2.Length - 1), 1);
            }
            return text;
        }
免责声明:本站所有资源均由网络搜集用户投稿而来,若有侵权,违法违规侵权内容请联系本站并提供必要证明,本站将及时删除违法违规内容,谢谢合作!