As the other answers have explained, a GUID
only guarantees uniqueness, not randomness.
If uniqueness is all that you need then the code in your question is fine, although you could just use Guid.NewGuid().ToString("N")
rather than explicitly replacing the hyphens.
If you do actually need a random string, then try something like this:
// use RNGCryptoServiceProvider instead of Random if you need extra security
private readonly Random _rng = new Random();
public string GetRandomString(int length)
{
const string allowedChars =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char[] buffer = new char[length];
for (int i = 0; i < length; i++)
{
buffer[i] = allowedChars[_rng.Next(allowedChars.Length)];
}
return new string(buffer);
}