Guid的带参数的ToString()方法来实现格式化,如下:
//// 摘要: // 根据所提供的格式说明符,返回此 System.Guid 实例值的字符串表示形式。 //// 参数: // format: // 一个单格式说明符,它指示如何格式化此 System.Guid 的值。format 参数可以是“N”、“D”、“B”、“P”或“X”。如果 format 为 // null 或空字符串 (""),则使用“D”。 //// 返回结果: // 此 System.Guid 的值的字符串表示形式。 //// 异常: // T:System.FormatException: // format 的值不为 null、空字符串 ("")、"N"、"D"、"B"、"P" 或 "X"。 public string ToString(string format);
示例如下:
Guid guid = Guid.NewGuid(); //{ee69e211-a55e-4211-af69-e046c293c39e} string a = guid.ToString("N"); //ee69e211a55e4211af69e046c293c39e string b = guid.ToString("D"); //ee69e211-a55e-4211-af69-e046c293c39e string c = guid.ToString("B"); //{ee69e211-a55e-4211-af69-e046c293c39e} string d = guid.ToString("P"); //(ee69e211-a55e-4211-af69-e046c293c39e) string e = guid.ToString("X"); //{0xee69e211,0xa55e,0x4211,{0xaf,0x69,0xe0,0x46,0xc2,0x93,0xc3,0x9e}}