Most concise form is
string guid = Guid.NewGuid().ToString("N");There are other formats as well The ToString(string format) method can format a guid in one of several ways:
"N" - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (32 digits) "D" - xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (32 digits separated by hyphens) "B" - {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (same as "D" with addition of braces) "P" - (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) (same as "D" with addition of parentheses) "X" - {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}The Guid can be shortened as a string by using Base64 encoding:
//Compress the given Guid as a base64 string that is 22 characters long. private static string ToShortString(Guid guid) { string encoded = Convert.ToBase64String(guid.ToByteArray()); return encoded.Substring(0, 22); // Last 2 characters are always == }
No comments:
Post a Comment