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

源码:

  #region 生成指定长度的随机字符串
/// <summary>
/// 生成指定长度的随机字符串
/// </summary>
/// <param name="intLength">随机字符串长度</param>
/// <param name="booNumber">生成的字符串中是否包含数字</param>
/// <param name="booSign">生成的字符串中是否包含符号</param>
/// <param name="booSmallword">生成的字符串中是否包含小写字母</param>
/// <param name="booBigword">生成的字符串中是否包含大写字母</param>
/// <returns></returns>
public string GetRandomizer(int intLength, bool booNumber, bool booSign, bool booSmallword, bool booBigword)
{
//定义
Random ranA = new Random();
int intResultRound = ;
int intA = ;
string strB = ""; while (intResultRound < intLength)
{
//生成随机数A,表示生成类型
//1=数字,2=符号,3=小写字母,4=大写字母 intA = ranA.Next(, ); //如果随机数A=1,则运行生成数字
//生成随机数A,范围在0-10
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环 if (intA == && booNumber)
{
intA = ranA.Next(, );
strB = intA.ToString() + strB;
intResultRound = intResultRound + ;
continue;
} //如果随机数A=2,则运行生成符号
//生成随机数A,表示生成值域
//1:33-47值域,2:58-64值域,3:91-96值域,4:123-126值域 if (intA == && booSign == true)
{
intA = ranA.Next(, ); //如果A=1
//生成随机数A,33-47的Ascii码
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环 if (intA == )
{
intA = ranA.Next(, );
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + ;
continue;
} //如果A=2
//生成随机数A,58-64的Ascii码
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环 if (intA == )
{
intA = ranA.Next(, );
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + ;
continue;
} //如果A=3
//生成随机数A,91-96的Ascii码
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环 if (intA == )
{
intA = ranA.Next(, );
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + ;
continue;
} //如果A=4
//生成随机数A,123-126的Ascii码
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环 if (intA == )
{
intA = ranA.Next(, );
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + ;
continue;
} } //如果随机数A=3,则运行生成小写字母
//生成随机数A,范围在97-122
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环 if (intA == && booSmallword == true)
{
intA = ranA.Next(, );
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + ;
continue;
} //如果随机数A=4,则运行生成大写字母
//生成随机数A,范围在65-90
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环 if (intA == && booBigword == true)
{
intA = ranA.Next(, );
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + ;
continue;
}
}
return strB; }
#endregion

读取数据:

  private void button1_Click(object sender, EventArgs e)
{
foreach (var file in Directory.GetFiles("E:\\renew", "*.txt").Where(t => t.ToLower().EndsWith(".txt")).ToList())
{
using (StreamReader reader = new StreamReader(file, Encoding.UTF8))
{
string strLine = string.Empty;
while ((strLine = reader.ReadLine()) != null)
{
renewModel.Content = strLine;
renewBLL.Add(renewModel);
}
}
}
}

写入数据:

   private void button2_Click(object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(@"E:\test.txt", true, Encoding.UTF8);
List<string> list = new List<string>();
while ( == )
{
string str = "CCKD";
str += GetRandomizer(, true, false, false, true);
if (!list.Contains(str))
{
list.Add(str);
sw.WriteLine(str);
sw.Flush();
}
if (list.Count== )
break;
}
sw.Close();
}
04-29 03:48