C#中怎么生成和获取GUID

一个生成GUID的函数:

private string getGUID()
    {
        System.Guid guid = new Guid();
        guid = Guid.NewGuid();
        string str = guid.ToString();
        return str;
    }

随机生成如下字符串:
e92b8e30-a6e5-41f6-a6b9-188230a23dd2

格式说明:

System.Guid.NewGuid().ToString(format)  
   
格式说明符            
返回值的格式    
   
N 32位:
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
如:e92b8e30a6e541f6a6b9188230a23dd2
     
D 由连字符分隔的32位数字:                                     
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
如:e92b8e30-a6e5-41f6-a6b9-188230a23dd2
     
B 括在大括号中、由连字符分隔的32位数字:      
{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
如:{e92b8e30-a6e5-41f6-a6b9-188230a23dd2}
     
P 括在圆括号中、由连字符分隔的32位数字:        
(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
如:(e92b8e30-a6e5-41f6-a6b9-188230a23dd2)

posted @ 2011-06-01 14:59  俗雅冰山  阅读(1908)  评论(0编辑  收藏  举报