c# 生成n位某系统内唯一字符串

注意,仅适用于某一系统内,思路是用ascill码去累加,累加到一定次数重置,使用时需配合datetime去拼接,这样可以达到理论上就算在同一时间内同时触发,也能保证生成的码不重复。

下面直接贴代码。

using System;
using System.IO;

/// <summary>
/// 生成工具
/// </summary>
public class GuidIdUtil
{
    private static string seqPath = "seq.txt";//序列号配置文件

    /// <summary>
    /// 程序运行环境
    /// </summary>
    public enum Enviroment
    {
        /// <summary>
        /// webapi环境
        /// </summary>
        HTTP,

        /// <summary>
        /// 纯客户端
        /// </summary>
        CLIENT
    }

    /// <summary>
    /// 获取配置文件中的序列号(我们自己生成的,存的是ascll码,最后会转换成字符串)
    /// </summary>
    /// <param name="length">生成的随机数位数</param>
    /// <param name="enviroment">环境,具体分类见枚举</param>
    /// <returns></returns>
    public static string GetSeqsString(int length, Enviroment enviroment)
    {
        string path = string.Empty;
        switch (enviroment)
        {
            case Enviroment.HTTP:
                //纯客户端时,请把这句话注释掉,可能"System.Web.Hosting.HostingEnvironment"这个方法可能点不出来
                path = System.Web.Hosting.HostingEnvironment.MapPath(@"~/") + seqPath;
                break;
            case Enviroment.CLIENT:
                path = Directory.GetCurrentDirectory() + "/" + seqPath;
                break;
            default:
                break;
        }


        if (!File.Exists(path))
        {
            File.Create(path).Close();
            string tempStr = string.Empty;
            for (int i = 0; i < length; i++)
            {
                tempStr += "65|";
            }
            tempStr = tempStr.Substring(0, tempStr.Length - 1);
            File.WriteAllText(path, tempStr);//初始写入
        }

        string strRead = File.ReadAllText(path);
        string[] seqArr = strRead.Split('|');
        int[] seqArr_2 = new int[seqArr.Length];
        for (int i = 0; i < seqArr.Length; i++)
        {
            seqArr_2[i] = Convert.ToInt32(seqArr[i]);
        }

        string ss = AddIndex(seqArr_2);

        //保存数组
        string strWrite = string.Empty;
        for (int i = 0; i < seqArr_2.Length; i++)
        {
            strWrite += seqArr_2[i].ToString();
            if (i < seqArr_2.Length - 1)
            {
                strWrite += "|";
            }
        }
        File.WriteAllText(path, strWrite);
        return ss;
    }

    /// <summary>
    /// 给数组+数字(可递归)
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="index"></param>
    /// <param name="addNum"></param>
    private static void SetArr(ref int[] arr, int index)
    {
        if (IsAllFull(arr))//数组全部最大值的时候重置
        {
            //全重置
            ResSetArr(arr);
            index = arr.Length - 1;
        }
        if (index < 0)//最左边的时候
        {
            //全重置
            ResSetArr(arr);
            index = arr.Length - 1;
        }
        int sum = arr[index] + 1;
        if (sum > 122 && index == 0)//假如一次加的值大于122
        {
            SetArr(ref arr, arr.Length - 1);//到最右边
        }
        else if (sum > 122 && index != 0)
        {
            arr[index] = 65;
            SetArr(ref arr, index - 1);//往左边挪一位
        }
        else
        {
            arr[index] = sum;
        }
    }

    /// <summary>
    /// 重置数组
    /// </summary>
    /// <param name="arr"></param>
    private static void ResSetArr(int[] arr)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = 65;
        }
    }

    /// <summary>
    /// 数组是否已经满了
    /// </summary>
    /// <param name="arr"></param>
    /// <returns></returns>
    private static bool IsAllFull(int[] arr)
    {
        bool res = false;
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] < 122)
            {
                break;
            }
            if (i == arr.Length - 1 && arr[i] >= 122)
            {
                res = true;
            }
        }
        return res;
    }

    /// <summary>
    /// 给文本中读取的序列号添加数字
    /// </summary>
    /// <param name="arr"></param>
    /// <param name="addNum"></param>
    /// <returns></returns>
    private static string AddIndex(int[] arr)
    {
        int sum = arr[arr.Length - 1] + 1;
        //满足122进1
        if (sum <= 122)
        {
            arr[arr.Length - 1] = sum;
        }
        else
        {
            SetArr(ref arr, arr.Length - 1);
        }

        string res = string.Empty;
        for (int i = 0; i < arr.Length; i++)
        {
            res += ((char)arr[i]).ToString();
        }
        return res;
    }
}

 

调用示例:

string s = GuidIdUtil.GetSeqsString(2, GuidIdUtil.Enviroment.HTTP) + DateTime.Now.ToString("yyyyMMddHHmmss");

 

posted on 2021-01-18 15:48  炼金师  阅读(92)  评论(0编辑  收藏  举报

导航