c# UUID生成
时间: 2023-11-22 20:50:06 浏览: 181
UUID generator
以下是在C#中生成UUID的两种方法:
1.使用Guid.NewGuid()方法生成随机UUID:
```csharp
Guid uuid = Guid.NewGuid();
string uuidStr = uuid.ToString().Replace("-", "");
Console.WriteLine(uuidStr);
```
2.使用MD5加密字符串生成固定UUID:
```csharp
using System.Security.Cryptography;
using System.Text;
public static string GetUUID(string name)
{
MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes(name);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 16; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
string uuidStr = GetUUID("example");
Console.WriteLine(uuidStr);
```
阅读全文