Kirtan Kirtan - 1 year ago 80
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.

Answer Source

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:


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download