c# 自动给版本升级,遇9变0且前面一个版本加1

    /// <summary>
    /// 版本辅助类
    /// 创建人:
    /// 创建时间:2019-11-18 13:53:55
    /// </summary>
    public class VersionHelper
    {
        /// <summary>
        /// 自动升级版本号,版本号+1
        /// </summary>
        /// <param name="version"></param>
        /// <returns></returns>
        private string AutoUpgradeVersion(string version)
        {
            if (string.IsNullOrEmpty(version))
            {
                version = "1.0.0";
            }
            //将版本号拆解成整数数组
            int[] ints = Array.ConvertAll<string, int>(version.Split('.'), s => Convert.ToInt32(s));

            //递归调用
            this.AutoUpgradeVersion(ints, ints.Length - 1);

            //数组转字符串
            version = string.Join(".", ints);

            return version;
        }

        /// <summary>
        /// 自动升级版本号,版本号+1
        /// </summary>
        /// <param name="ints"></param>
        /// <param name="index"></param>
        private void AutoUpgradeVersion(int[] ints, int index)
        {
            if (index == 0)
            {
                ints[0] = ints[0] + 1;
            }
            else
            {
                int value = ints[index] + 1;
                if (value < 10)
                {
                    ints[index] = value;
                }
                else
                {
                    ints[index] = 0;
                    AutoUpgradeVersion(ints, index - 1);
                }
            }
        }
    }

 

posted @ 2019-11-18 13:56  屌丝大叔的笔记  阅读(617)  评论(0编辑  收藏  举报