https://www.codeproject.com/articles/31236/how-to-update-assembly-version-number-automaticall

Examples

AssemblyInfoUtil.exe -set:3.1.7.53 "C:\Program Files\MyProject1\AssemblyInfo.cs"

Set the version string to "3.1.7.53".

AssemblyInfoUtil.exe -inc:4 AssemblyInfo.cs

Increase the last (revision) number. So in our case it will become 54.

using System;
using System.IO;
using System.Text; namespace AssemblyInfoUtil
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class AssemblyInfoUtil
{
private static int incParamNum = ; private static string fileName = ""; private static string versionStr = null; private static bool isVB = false; /// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
for (int i = ; i < args.Length; i++) {
if (args[i].StartsWith("-inc:")) {
string s = args[i].Substring("-inc:".Length);
incParamNum = int.Parse(s);
}
else if (args[i].StartsWith("-set:")) {
versionStr = args[i].Substring("-set:".Length);
}
else
fileName = args[i];
} if (Path.GetExtension(fileName).ToLower() == ".vb")
isVB = true; if (fileName == "") {
System.Console.WriteLine(@"Usage: AssemblyInfoUtil
<path to AssemblyInfo.cs or AssemblyInfo.vb file> [options]");
System.Console.WriteLine("Options: ");
System.Console.WriteLine(@" -set:<new version number> -
set new version number (in NN.NN.NN.NN format)");
System.Console.WriteLine(@" -inc:<parameter index> -
increases the parameter with specified index (can be from 1 to 4)");
return;
} if (!File.Exists(fileName)) {
System.Console.WriteLine
("Error: Can not find file \"" + fileName + "\"");
return;
} System.Console.Write("Processing \"" + fileName + "\"...");
StreamReader reader = new StreamReader(fileName);
StreamWriter writer = new StreamWriter(fileName + ".out");
String line; while ((line = reader.ReadLine()) != null) {
line = ProcessLine(line);
writer.WriteLine(line);
}
reader.Close();
writer.Close(); File.Delete(fileName);
File.Move(fileName + ".out", fileName);
System.Console.WriteLine("Done!");
} private static string ProcessLine(string line) {
if (isVB) {
line = ProcessLinePart(line, "<Assembly: AssemblyVersion(\"");
line = ProcessLinePart(line, "<Assembly: AssemblyFileVersion(\"");
}
else {
line = ProcessLinePart(line, "[assembly: AssemblyVersion(\"");
line = ProcessLinePart(line, "[assembly: AssemblyFileVersion(\"");
}
return line;
} private static string ProcessLinePart(string line, string part) {
int spos = line.IndexOf(part);
if (spos >= ) {
spos += part.Length;
int epos = line.IndexOf('"', spos);
string oldVersion = line.Substring(spos, epos - spos);
string newVersion = "";
bool performChange = false; if (incParamNum > ) {
string[] nums = oldVersion.Split('.');
if (nums.Length >= incParamNum && nums[incParamNum - ] != "*") {
Int64 val = Int64.Parse(nums[incParamNum - ]);
val++;
nums[incParamNum - ] = val.ToString();
newVersion = nums[];
for (int i = ; i < nums.Length; i++) {
newVersion += "." + nums[i];
}
performChange = true;
}
} else if (versionStr != null) {
newVersion = versionStr;
performChange = true;
} if (performChange) {
StringBuilder str = new StringBuilder(line);
str.Remove(spos, epos - spos);
str.Insert(spos, newVersion);
line = str.ToString();
}
}
return line;
}
}
}

预先生成事件命令行:(对 AssemblyVersion 和 AssemblyFileVersion 属性的最后一位执行+1处理)

if $(ConfigurationName) == Release (
call $(SolutionDir)AssemblyInfoUtil.exe -inc:4 $(ProjectDir)Properties\AssemblyInfo.cs
)


或者使用VS插件也是一个不错的选择:

Automatic Versions

.net 项目生成时自动更新版本号的更多相关文章

  1. 扩展BindingList,防止增加、删除项时自动更新界面而不出现“跨线程操作界面控件 corss thread operation”异常

    在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应 ...

  2. winform datagridview 不显示默认第一列 不显示未绑定列 数据源发生改变时自动更新 (转)

    不显示带星号的第一列: datagridview属性框中将 RowHeadersVisiber 设置为  false 不显示未绑定列: datagridview有一个属性是 AutoGenerateC ...

  3. Android Studio生成APK自动追加版本号、自定义apk名称、指定签名证书文件

    你也可以查看我的其他同类文章,也会让你有一定的收货! 生成APK自动追加版本号 可自动区分debug和release,并追加版本号: 打开 build.gradle 在 android 节点中插入下面 ...

  4. [Azure DevOps] 编译时自动修改版本号

    1. 需求 在使用 Pipeline 自动化 CI/CD 流程的过程中,我还还需要自动修改程序集的版本号.这个功能 EdiWang 和LeoLaw 都写文章讲解过做法.不过我的项目基本都是 .Net ...

  5. 神奇的bug,退出时自动更新时间

    遇到一个神奇的bug,用户退出时,上次登录时间会变成退出时的时间. 于是开始跟踪,发现Laravel在退出时,会做一次脏检查,这时会更新rember_token,这时就会有update操作如下. 而粗 ...

  6. mysql设置updatetime字段每次修改时自动更新

    我们在数据库表设计阶段中都会加上CreateTime, UpdateTime字段, 在重要业务字段更新的时候,都会重新赋值UpdateTime字段,这个对后期查找分析业务数据变更时非常有用. 但是现在 ...

  7. (转载)让XCode运行时自动更新资源

    转自http://goldlion.blog.51cto.com/4127613/1351616 用过XCode的人都知道,XCode有一个臭名昭著的bug——除非你修改了源代码造成了重新编译,否则游 ...

  8. [QuickX]xcode运行Quick-cocos2d-x项目时自动更新lua资源文件

    1.项目设置 build settings ->build options ->Scan all source files and Includes = YES 2.加入script (1 ...

  9. VS2010 win7 64位安装后新建项目生成时错误:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏

    解决方案:VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK1123” 错误,解决方案为将 项目|项目属性|配置属性|清单工具|输入和输出|嵌入清单 ...

随机推荐

  1. Three.js制作360度全景图

    这是个基于three.js的插件,预览地址:戳这里 使用方法: 1.这个插件的用法很简单,引入如下2个js <script src="js/three.min.js"> ...

  2. sed的应用

    h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ...

  3. Vagrant基础简要记录

    Vagrant是一种开源软件,它为跨众多操作系统构建可重复的开发环境提供了一种方法.Vagrant使用提供者(provider)来启动隔离的虚拟环境.默认的提供者是Virtualbox Vagrant ...

  4. 高仿ios版美团框架项目源码

    高仿美团框架基本已搭好.代码简单易懂,适合新人.适合新人.新人. <ignore_js_op>     源码你可以到ios教程网那里下载吧,这里我就不上传了,http://ios.662p ...

  5. Mac电脑如何读取Android手机上的文件

    问题 一般Android手机用usb数据线连接到windows操作系统的电脑上后,会自动将手机存储卡以移动存储的方式显示在电脑里. 但是如果操作系统是Mac的,就没有这个存储设备.问题来了,Mac电脑 ...

  6. [Erlang 0110] Erlang Abstract Format , Part 1

    Erlang Abstract Format并不难懂,只是枯燥一点罢了,如果把Abstract Format的文档翻译出来,其实就是Erlang教科书中语法入门的部分. Erlang Abstract ...

  7. Scala基础语法 (一)

    如果你之前是一名 Java 程序员,并了解 Java 语言的基础知识,那么你能很快学会 Scala 的基础语法. Scala 与 Java 的最大区别是:Scala 语句末尾的分号 ; 是可选的. 我 ...

  8. CSS3:radial-gradient,径向渐变的使用方法

    语法 径向渐变不同于线性渐变,线性渐变是从“一个方向”向“另一个方向”的颜色渐变,而径向渐变是从“一个点”向四周的颜色渐变.其语法如下: background: radial-gradient(cen ...

  9. kvm常用操作

    安装一些虚拟化的组件 yum -y install kvm python-virtinst libvirt bridge-utils virt-manager qemu-kvm-tools virt- ...

  10. x01.CodeBuilder: 生成代码框架

    根据 Assembly 生成代码框架. 这是学习 AvalonEdit 的一个副产品.学习时,照着源代码新建文件夹,新建文件,添加方法与属性,虽然只是个框架,也要花费大量时间.为什么不让它自动生成呢? ...