坐峰怀雪灬

路漫漫其修远兮,吾将上下而求索。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

(C#)生成指定长度的随机字符串的通用方法

Posted on 2017-03-01 15:20  坐峰怀雪灬  阅读(4747)  评论(0编辑  收藏  举报

.NET(C#)生成指定长度的随机字符串的通用方法,此方法可以指定字符串的长度,是否包含数字,是否包含符号,是否包含小写字母,是否包含大写字母等,

源码:

  1  #region 生成指定长度的随机字符串
  2         /// <summary>
  3         /// 生成指定长度的随机字符串
  4         /// </summary>
  5         /// <param name="intLength">随机字符串长度</param>
  6         /// <param name="booNumber">生成的字符串中是否包含数字</param>
  7         /// <param name="booSign">生成的字符串中是否包含符号</param>
  8         /// <param name="booSmallword">生成的字符串中是否包含小写字母</param>
  9         /// <param name="booBigword">生成的字符串中是否包含大写字母</param>
 10         /// <returns></returns>
 11         public string GetRandomizer(int intLength, bool booNumber, bool booSign, bool booSmallword, bool booBigword)
 12         {
 13             //定义  
 14             Random ranA = new Random();
 15             int intResultRound = 0;
 16             int intA = 0;
 17             string strB = "";
 18 
 19             while (intResultRound < intLength)
 20             {
 21                 //生成随机数A,表示生成类型  
 22                 //1=数字,2=符号,3=小写字母,4=大写字母  
 23 
 24                 intA = ranA.Next(1, 5);
 25 
 26                 //如果随机数A=1,则运行生成数字  
 27                 //生成随机数A,范围在0-10  
 28                 //把随机数A,转成字符  
 29                 //生成完,位数+1,字符串累加,结束本次循环  
 30 
 31                 if (intA == 1 && booNumber)
 32                 {
 33                     intA = ranA.Next(0, 10);
 34                     strB = intA.ToString() + strB;
 35                     intResultRound = intResultRound + 1;
 36                     continue;
 37                 }
 38 
 39                 //如果随机数A=2,则运行生成符号  
 40                 //生成随机数A,表示生成值域  
 41                 //1:33-47值域,2:58-64值域,3:91-96值域,4:123-126值域  
 42 
 43                 if (intA == 2 && booSign == true)
 44                 {
 45                     intA = ranA.Next(1, 5);
 46 
 47                     //如果A=1  
 48                     //生成随机数A,33-47的Ascii码  
 49                     //把随机数A,转成字符  
 50                     //生成完,位数+1,字符串累加,结束本次循环  
 51 
 52                     if (intA == 1)
 53                     {
 54                         intA = ranA.Next(33, 48);
 55                         strB = ((char)intA).ToString() + strB;
 56                         intResultRound = intResultRound + 1;
 57                         continue;
 58                     }
 59 
 60                     //如果A=2  
 61                     //生成随机数A,58-64的Ascii码  
 62                     //把随机数A,转成字符  
 63                     //生成完,位数+1,字符串累加,结束本次循环  
 64 
 65                     if (intA == 2)
 66                     {
 67                         intA = ranA.Next(58, 65);
 68                         strB = ((char)intA).ToString() + strB;
 69                         intResultRound = intResultRound + 1;
 70                         continue;
 71                     }
 72 
 73                     //如果A=3  
 74                     //生成随机数A,91-96的Ascii码  
 75                     //把随机数A,转成字符  
 76                     //生成完,位数+1,字符串累加,结束本次循环  
 77 
 78                     if (intA == 3)
 79                     {
 80                         intA = ranA.Next(91, 97);
 81                         strB = ((char)intA).ToString() + strB;
 82                         intResultRound = intResultRound + 1;
 83                         continue;
 84                     }
 85 
 86                     //如果A=4  
 87                     //生成随机数A,123-126的Ascii码  
 88                     //把随机数A,转成字符  
 89                     //生成完,位数+1,字符串累加,结束本次循环  
 90 
 91                     if (intA == 4)
 92                     {
 93                         intA = ranA.Next(123, 127);
 94                         strB = ((char)intA).ToString() + strB;
 95                         intResultRound = intResultRound + 1;
 96                         continue;
 97                     }
 98 
 99                 }
100 
101                 //如果随机数A=3,则运行生成小写字母  
102                 //生成随机数A,范围在97-122  
103                 //把随机数A,转成字符  
104                 //生成完,位数+1,字符串累加,结束本次循环  
105 
106                 if (intA == 3 && booSmallword == true)
107                 {
108                     intA = ranA.Next(97, 123);
109                     strB = ((char)intA).ToString() + strB;
110                     intResultRound = intResultRound + 1;
111                     continue;
112                 }
113 
114                 //如果随机数A=4,则运行生成大写字母  
115                 //生成随机数A,范围在65-90  
116                 //把随机数A,转成字符  
117                 //生成完,位数+1,字符串累加,结束本次循环  
118 
119                 if (intA == 4 && booBigword == true)
120                 {
121                     intA = ranA.Next(65, 89);
122                     strB = ((char)intA).ToString() + strB;
123                     intResultRound = intResultRound + 1;
124                     continue;
125                 }
126             }
127             return strB;
128 
129         }
130         #endregion

读取数据:

 1  private void button1_Click(object sender, EventArgs e)
 2         {
 3             foreach (var file in Directory.GetFiles("E:\\renew", "*.txt").Where(t => t.ToLower().EndsWith(".txt")).ToList())
 4             {
 5                 using (StreamReader reader = new StreamReader(file, Encoding.UTF8))
 6                 {
 7                     string strLine = string.Empty;
 8                     while ((strLine = reader.ReadLine()) != null)
 9                     {
10                         renewModel.Content = strLine;
11                         renewBLL.Add(renewModel);
12                     }
13                 }
14             }
15         }

写入数据:

 1   private void button2_Click(object sender, EventArgs e)
 2         {
 3             StreamWriter sw = new StreamWriter(@"E:\test.txt", true, Encoding.UTF8);
 4             List<string> list = new List<string>();
 5             while (1 == 1)
 6             {
 7                 string str = "CCKD";
 8                 str += GetRandomizer(5, true, false, false, true);
 9                 if (!list.Contains(str))
10                 {
11                     list.Add(str);
12                     sw.WriteLine(str);
13                     sw.Flush();
14                 }
15                 if (list.Count== 100000)
16                     break;
17             }
18             sw.Close();
19         }