Monday, May 27, 2013

C#(Generate Random String)

private static string GeneratePassword()
        {
            Random rd = new Random(DateTime.Now.Millisecond);
            string allowedCharsAll = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            string allowedCharsUpper = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
            string allowedCharsNum = "0123456789";
            int passwordLength = rd.Next(8, 8);
            char[] chars = new char[passwordLength];

            chars[0] = allowedCharsUpper[rd.Next(0, allowedCharsUpper.Length)];
            chars[1] = allowedCharsNum[rd.Next(0, allowedCharsNum.Length)];

            for (int i = 2; i < passwordLength; i++)
            {
                chars[i] = allowedCharsAll[rd.Next(0, allowedCharsAll.Length)];
            }

            return new string(chars);
        }


No comments:

Post a Comment