123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Text;
- namespace WingDeviceService.Common
- {
- public class CodeCreator
- {
- private CodeCreator(){}
- /// <summary>
- /// 生成单个随机数字
- /// </summary>
- private static int CreateNum()
- {
- Random random = new Random(Guid.NewGuid().GetHashCode());
- int num = random.Next(10);
- return num;
- }
-
- /// <summary>
- /// 生成单个大写随机字母
- /// </summary>
- private static string CreateBigAbc()
- {
- //A-Z的 ASCII值为65-90
- Random random = new Random(Guid.NewGuid().GetHashCode());
- int num = random.Next(65, 91);
- string abc = Convert.ToChar(num).ToString();
- return abc;
- }
-
- /// <summary>
- /// 生成单个小写随机字母
- /// </summary>
- private static string CreateSmallAbc()
- {
- //a-z的 ASCII值为97-122
- Random random = new Random(Guid.NewGuid().GetHashCode());
- int num = random.Next(97, 123);
- string abc = Convert.ToChar(num).ToString();
- return abc;
- }
- /// <summary>
- /// 生成随机字符串
- /// </summary>
- /// <param name="length">字符串的长度</param>
- /// <returns></returns>
- public static string CreateRandomStr(int length)
- {
- // 创建一个StringBuilder对象存储密码
- StringBuilder sb = new StringBuilder();
- //使用for循环把单个字符填充进StringBuilder对象里面变成14位密码字符串
- for (int i = 0; i < length; i++)
- {
- Random random = new Random(Guid.NewGuid().GetHashCode());
- //随机选择里面其中的一种字符生成
- switch (random.Next(3))
- {
- case 0:
- //调用生成生成随机数字的方法
- sb.Append(CreateNum());
- break;
- case 1:
- //调用生成生成随机小写字母的方法
- sb.Append(CreateSmallAbc());
- break;
- case 2:
- //调用生成生成随机大写字母的方法
- sb.Append(CreateBigAbc());
- break;
- }
- }
- return sb.ToString();
- }
- /// <summary>
- /// 创建编码
- /// </summary>
- /// <param name="prefix"></param>
- /// <returns></returns>
- public static string CreateCode(string prefix)
- {
- return WingServerCommon.Utilities.IdHelper.Generate<string>();
- }
- }
- }
|