Tuesday, 21 April 2015

Generate Random String using Asp.net c#




//Using For loop

public void Generate_Random_String()
    {
        char[] charSet =  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".ToCharArray();
        string Random_String = "";
        Random RandomNo = new Random();
        byte[] RandomBytes = new byte[32];
        RandomNo.NextBytes(RandomBytes);
       
        for (int i = 0; i < 32; i++)
            Random_String = Random_String + Convert.ToString(charSet[RandomBytes[i] % 62]);
     

    }


//Othere
    public void Generate_RandomString()
    {

        var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        var random = new Random();
        var result = new string(Enumerable.Repeat(chars, 30) .Select(s => s[random.Next(s.Length)]).ToArray())+DateTime.Now.ToString("yyyyMMddhhmmss");

    }


//Result=jdu3tbdi8596mMHFROP20150412042010

No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...