获取 Guid 的字符串表示
可以使用内置的 ToString
方法获得 Guid 的字符串表示
string myGuidString = myGuid.ToString();
根据你的需要,你还可以通过向 ToString
调用添加格式类型参数来格式化 Guid。
var guid = new Guid("7febf16f-651b-43b0-a5e3-0da8da49e90d");
// None "7febf16f651b43b0a5e30da8da49e90d"
Console.WriteLine(guid.ToString("N"));
// Hyphens "7febf16f-651b-43b0-a5e3-0da8da49e90d"
Console.WriteLine(guid.ToString("D"));
// Braces "{7febf16f-651b-43b0-a5e3-0da8da49e90d}"
Console.WriteLine(guid.ToString("B"));
// Parentheses "(7febf16f-651b-43b0-a5e3-0da8da49e90d)"
Console.WriteLine(guid.ToString("P"));
// Hex "{0x7febf16f,0x651b,0x43b0{0xa5,0xe3,0x0d,0xa8,0xda,0x49,0xe9,0x0d}}"
Console.WriteLine(guid.ToString("X"));