Generates a random string - CSharp System

CSharp examples for System:Random

Description

Generates a random string

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License");
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Generic;
using System;/*from   www.j  a  v  a  2  s  .  co m*/

public class Main{
        /// <summary>
        /// Generates a random string 
        /// </summary>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string RandomString(int length) {
            byte[] randBuffer = new byte[length];
            RandomNumberGenerator.Create().GetBytes(randBuffer);
            return System.Convert.ToBase64String(randBuffer).Remove(length);
        }
}

Related Tutorials