Sunday, November 22, 2009

Generate a random string based on length in c#

The following method will generate a random string using c#,

public string GetRandomPasswordUsingGUID(int length)
{
string guidResult = System.Guid.NewGuid().ToString();
guidResult = guidResult.Replace("-", string.Empty);
if (length <= 0 || length > guidResult.Length)
throw new ArgumentException("Length must be between 1 and " + guidResult.Length);
return guidResult.Substring(0, length);
}

No comments:

Post a Comment