有如何生成GUID的方法如下?

use*_*949 -1 c# guid

如何生成如下所示的GUID: 00000000-0000-0000-0000-B7B9B3A4A0DE

除了最后一部分,它应该都是零.

Jon*_*eet 9

你的意思并不清楚.例如,这有效:

Guid guid = Guid.Parse("00000000-0000-0000-0000-B7B9B3A4A0DE");
Run Code Online (Sandbox Code Playgroud)

因此,您可以毫无问题地从文本转换为Guid.如果你想生成这样的Guid,你总是可以生成6个随机字节(例如with RandomNumberGenerator)并传入适当的字节数组Guid(byte[]).例如:

using System;
using System.Security.Cryptography;

class Test
{
    static void Main()
    {
        var rng = RandomNumberGenerator.Create();
        byte[] randomBytes = new byte[6];
        rng.GetBytes(randomBytes);

        byte[] guidBytes = new byte[16];
        Buffer.BlockCopy(randomBytes, 0, guidBytes, 10, 6);

        Guid guid = new Guid(guidBytes);
        Console.WriteLine(guid);
    }
}
Run Code Online (Sandbox Code Playgroud)