C#项目版本号自定义位置自动向上增加小工具设计与实现

博客 动态
0 255
羽尘
羽尘 2022-02-16 18:55:26
悬赏:0 积分 收藏

C#项目版本号自定义位置自动向上增加小工具设计与实现

  自从毕了业,好久没更新了,今天突发奇想,过来更新一下,嘻嘻!

  一般在做版本升级时,要锁定版本号进行对比,然后联网检索可用的升级包信息,在用VS做C#项目组件版本管理时,是一个很麻烦的事,每次Release之前都要硬核的去改AssemblyInfo.cs文件的版本信息,采用VS官方推荐的[assembly: AssemblyVersion("1.0.*")]格式最后生成的版本号是1.0.1589.39645之类的,留给自定义的只剩下前两位,对于有个性的开发者明显彰显不出咱们的个性,所以下面和大家说一个相对来说比较好用的方法或逻辑,假设能有一个小程序在每次Release之前,去自动修改AssemblyInfo.cs文件中对应的版本号,代替我们手动去修改,修改的值为某年某月某日到今天发布的累计小时数,就达到了自动修改并向上叠加的目的,废话不多说,上正文。

  右击项目属性,选择“生成事件”,输入如下命令:

  

1 if $(ConfigurationName)==Release 2 (3     "$(SolutionDir)AutoVersion.exe" $(ProjectDir)4 )

  如下图所示

 

  其中的参数意义,可点击“编辑预先生成”按钮后,打开编辑界面,点击“宏”按钮查看相应变量的值,此处不做过多的解释,只需要记住这个是用于调用AutoVersion.exe程序并把当前项目文件的根路径作为参数传进去的作用就行了,

  下面说一下AutoVersion.exe程序的具体逻辑,首先获取AssemblyInfo.cs问价的路径,然后读取该文件中的AssemblyVersion的值,并对其进行修改,然后重新写入到文件中,具体代码如下:

  

 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 using System.Xml; 7  8 namespace AutoVersion 9 {10     class Program11     {12         static void Main(string[] args)13         {14             try15             {16                 if (args.Length <= 0)17                     return;18                 if (!Directory.Exists(args[0]))19                     return;20                 string strPath = Path.Combine(args[0] + "/Properties/AssemblyInfo.cs");22                 if (File.Exists(strPath))23                 {24                     string[] strinfo = File.ReadAllLines(strPath, Encoding.UTF8);25                     DateTime startTime = new DateTime(2020, 1, 1);26                     TimeSpan ts = DateTime.Now - startTime;27                     int littleVersion = ts.Hours + ts.Days * 24;28                     29                     for (int i = 0; i < strinfo.Length; i++)30                     {31                         string startStr = "AssemblyVersion(\"";32                         string endStr = "\")]";33                         string pointStr = ".";34                         if (strinfo[i] != "" && strinfo[i].Contains(startStr) && strinfo[i].Contains(endStr) && strinfo[i].Contains(pointStr))35                         {36                             string[] strArr = strinfo[i].Split('.');37                             if (strArr != null && strArr.Length == 4)38                             {39                                 int startIndex = strinfo[i].LastIndexOf(pointStr) + 1;40                                 int endIndex = strinfo[i].IndexOf(endStr);41                                 if (endIndex > startIndex)42                                 {43                                     string headerStr = strinfo[i].Substring(0, startIndex);44                                     string enderStr = strinfo[i].Substring(endIndex, strinfo[i].Length - endIndex);45                                     strinfo[i] = headerStr + littleVersion + enderStr;46                                 }47                             }48                         }49                     }50                     File.WriteAllLines(strPath, strinfo, Encoding.UTF8);51                 }52             }53             catch (Exception ex)54             {55                 Console.WriteLine(ex.ToString());56             }57         }58     }59 }

  Release生成AutoVersion.exe文件,放到 $(SolutionDir) 的值所在的文件夹内,重新Release配置好的项目,即可修改相应的值,结果如下图:

  

 

posted @ 2022-02-16 18:02 thyou 阅读(29) 评论(0) 编辑 收藏 举报
回帖
    羽尘

    羽尘 (王者 段位)

    2335 积分 (2)粉丝 (11)源码

     

    温馨提示

    亦奇源码

    最新会员