二维码随机生成

来源:互联网 发布:快递入库软件app 编辑:程序博客网 时间:2024/06/11 05:11

#region 验证码长度(默认20个验证码的长度)
       
        int length = 20;
        public int Length
        {
            get { return length; }
            set { length = value; }
        }
       
        #endregion


        #region 生成随机字符码(二维码)

        public string CreateVerifyCode(int codeLen)
        {
            if (codeLen == 0)
            {
                codeLen = this.Length;
            }

            string[] arr = { "0","1"};

            string code = "";

            int randValue = -1;

            Random rand = new Random(unchecked((int)DateTime.Now.Ticks));

            for (int i = 0; i < codeLen; i++)
            {
                randValue = rand.Next(arr.Length);

                code += arr[randValue];
            }

            return code;
        }

        public string CreateVerifyCode()
        {
            return CreateVerifyCode(0);
        }

        #endregion