Saturday, May 23, 2015

Create Zip Rar And Gzip Files Using .Net

public static string GenerateRandomString(int length)
{
//It will generate string with combination of small,capital letters and numbers
    char[] charArr ="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
    string randomString = string.Empty;
    Random objRandom = new Random();
    for (int i = 0; i < length; i++)
    {
        //Don't Allow Repetation of Characters
        int x = objRandom.Next(1, charArr.Length);
        if (!randomString.Contains(charArr.GetValue(x).ToString()))
            randomString += charArr.GetValue(x);
        else
            i--;
    }
    return randomString;
}

To generate the random string with only capital letters then change charArr[] to below.
// to generate random string only with capital letters
char[] charArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

To generate the random string with only small  letters then change charArr[] to below.
// to generate random string only with small letters
char[] charArr = "abcdefghijklmnopqrstuvwxyz".ToCharArray();