Kirtan Kirtan - 2 months ago 9
C# Question

Unique random string generation

I'd like to generate random unique strings like the ones being generated by MSDN library:, for example. A string like 't9zk6eay' should be generated.


Using Guid would be a pretty good way, but to get something looking like your example, you probably want to convert it to a Base64 string:

    Guid g = Guid.NewGuid();
    string GuidString = Convert.ToBase64String(g.ToByteArray());
    GuidString = GuidString.Replace("=","");
    GuidString = GuidString.Replace("+","");

I get rid of "=" and "+" to get a little closer to your example, otherwise you get "==" at the end of your string and a "+" in the middle. Here's an example output string: