C# 更新AssemblyInfo.cs的文件版本号(AssemblyFileVersion)

45 篇文章 0 订阅
33 篇文章 0 订阅

       最近由于单位发布测试密度较大,因此手动更新每个程序集([assembly: AssemblyFileVersion("1.0.0.0")])的版本号,成了很头痛的事情,因此想着能自动在原有的基础上进行累加。

       因为每个人用到的版本信息可能不是同一个值,仅以我自己用到的来举例[assembly: AssemblyFileVersion("1.0.0.0")],我修改的是第二位(标红的),话不多说直接上代码:

  1. 首先是要选取路径(比较简单不进行赘述)
  2. 需要使用递归进行逐层进行文件筛选
  3. 调用端代码 
    private void GetFiles(DirectoryInfo dirInfo)
     {
                try
                {
                    FileInfo[] allFile = dirInfo.GetFiles();
                    foreach (FileInfo fileInfo in allFile)
                    {
                        ReadFile(fileInfo.FullName);
                    }
                    DirectoryInfo[] allDir = dirInfo.GetDirectories();
                    foreach (DirectoryInfo dir in allDir)
                    {
                        GetFiles(dir);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("更新版本号失败!");
                }
    }
    ------------------------------------------------------------------------------------------------------------------------------------------

 private void GetFiles(DirectoryInfo dirInfo)

{
            try
            {
                FileInfo[] allFile = dirInfo.GetFiles();
                foreach (FileInfo fileInfo in allFile)
                {
                    ReadFile(fileInfo.FullName);
                }
                DirectoryInfo[] allDir = dirInfo.GetDirectories();
                foreach (DirectoryInfo dir in allDir)
                {
                    GetFiles(dir);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

  private void ReadFile(string targetPath)
        {
            if (!string.IsNullOrEmpty(targetPath) && !targetPath.Contains("AssemblyInfo.cs"))
            {
                return;
            }
            string pVersionName = "AssemblyFileVersion";
            string content = File.ReadAllText(targetPath);
            int markCount = GetMarkCountFromAllContent(content, pVersionName);
            for (int i = 0; i < markCount; i++)
            {
                string[] arrayVersion;
                string stitchingVersionString;
                int versionIndex = content.IndexOf(pVersionName);
                string assemblyVersion = content.Substring(versionIndex);
                arrayVersion = assemblyVersion.Split('.');
                if (arrayVersion.Length > 2)
                {
                    arrayVersion[0] = arrayVersion[0].Insert(1, "##");
                    int assemblyVersionNumber = 0;
                    int.TryParse(arrayVersion[1], out assemblyVersionNumber);
                    arrayVersion[1] = (assemblyVersionNumber + 1).ToString();
                    var host = arrayVersion[0].Substring(arrayVersion[0].LastIndexOf("(") + 2);
                    int perVer = assemblyVersionNumber;
                    string hostVersion = host + "." + arrayVersion[1];
                    string perHostVersion = host + "." + perVer;
                    int index = targetPath.IndexOf("Properties");

                    var assimbly = targetPath.Substring(0, index - 1);
                    int inde = assimbly.LastIndexOf('\\');
                    var TypeName = assimbly.Substring(inde + 1);
                    if (!lstVersion.Contains(TypeName + ".dll       版本:" + hostVersion))
                    {
                        lstVersion.Add(TypeName + ".dll       版本:" + hostVersion);
                        txtShowInfo.Text += TypeName + ".dll       版本:" + hostVersion + "      更新前版本:" + perHostVersion + System.Environment.NewLine;
                    }
                }
                stitchingVersionString = "";
                int isFirst = 0;
                foreach (string version in arrayVersion)
                {
                    if (isFirst == 0)
                    {
                        stitchingVersionString += version + ".";
                        isFirst++;
                    }
                    else if (isFirst == 1)
                    {
                        stitchingVersionString += version;
                        isFirst++;
                    }
                    else
                    {
                        stitchingVersionString += "." + version;
                        isFirst++;
                    }
                }
                string finishContent = content.Substring(0, versionIndex);
                finishContent += stitchingVersionString;
                content = finishContent;
                content = content.Replace("##", "");
                File.WriteAllText(targetPath, content);
            }
        }

 /// <summary>
        /// 获取打开文档中存在几处标记的数量
        /// </summary>
        /// <param name="pAllContent">打开文档的全部内容</param>
        /// <param name="pMark">标记</param>
        /// <returns></returns>
        public int GetMarkCountFromAllContent(string pAllContent, string pMark)
        {
            if (pAllContent.Contains(pMark))
            {
                string strReplaced = pAllContent.Replace(pMark, "");
                return (pAllContent.Length - strReplaced.Length) / pMark.Length;
            }
            else
            {
                return 0;
            }
        }

 /// <summary>
        /// 读取文件路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReadPath_Click(object sender, EventArgs e)
        {
            if (fbDFolderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                lstVersion.Clear();
                string path = fbDFolderBrowserDialog.SelectedPath;
                txtPath.Text = path;
            }
        }

实现的功能比较单一,且通用性较差,后续继续优化。

有新的想法的小伙伴麻烦在评论区给出建议,或贴出优化后的代码..不胜感激

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值