生成任意位数随机字符串
/// <summary> /// 产生随机字符串 /// </summary> /// <param name="num">随机出几个字符</param> /// <returns>随机出的字符串</returns> private string GenCode(int num) { string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //char[] chastr = str.ToCharArray(); //第二种 string code = ""; Random rd = new Random(); int i; for (i = 0; i < num; i++) { //code += chastr[rd.Next(0, chastr.Length)]; //第二种 code += str.Substring(rd.Next(0, str.Length), 1); //第一种 } return code; }
最后调用即可,想出现几位就写几位
string checkCode = GenCode(6);