C# GUID

public class GuidHelper
{
    /// <summary>
    /// 格式一(“D”或“d”):51f2ec38-105c-47f7-8657-82d8375e847c
    /// </summary>
    public static string GetGuidDString()
    {
        return Guid.NewGuid().ToString("D");
    }
    /// <summary>
    /// 格式二(“N”或“n”):f54074a27b6e4406ae38c7944c44ad50
    /// </summary>
    public static string GetGuidNString()
    {
        return Guid.NewGuid().ToString("N");
    }
    /// <summary>
    /// 格式三(“P”或“p”):(c17298bf-954b-44ad-8e32-4fb598ce7e8f)
    /// </summary>
    public static string GetGuidPString()
    {
        return Guid.NewGuid().ToString("P");
    }
    /// <summary>
    /// 格式四(“B”或“b”):{a8826af4-450d-4d6b-864b-cedc210fe04b}
    /// </summary>
    public static string GetGuidBString()
    {
        return Guid.NewGuid().ToString("B");
    }
    /// <summary>
    /// 格式五(“X”或“x”):(ade24d16-db0f-40af-8794-1e08e2040df3)
    /// </summary>
    public static string GetGuidXString()
    {
        return Guid.NewGuid().ToString("X");
    }
    /// <summary>
    /// 格式六(19位唯一字数字):5443303397224987016
    /// </summary>
    public static long GetGuidInt64()
    {
        byte[] buffer = Guid.NewGuid().ToByteArray();
        return BitConverter.ToInt64(buffer, 0);
    }
    /// <summary>
    /// 格式七(16位唯一字符串):7ea7abc4e78ed56a
    /// </summary>
    /// <returns></returns>
    public static string GuidTo16String()
    {
        long i = 1;
        foreach (byte b in Guid.NewGuid().ToByteArray())
            i *= ((int)b + 1);
        return string.Format("{0:x}", i - DateTime.Now.Ticks);
    }
}

  

posted @ 2021-06-29 17:04  microsoft-zhcn  阅读(76)  评论(0编辑  收藏  举报