.net 项目生成时自动更新版本号

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 = 0;

    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 = 0; 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 >= 0) {
        spos += part.Length;
        int epos = line.IndexOf(‘"‘, spos);
        string oldVersion = line.Substring(spos, epos - spos);
        string newVersion = "";
        bool performChange = false;

        if (incParamNum > 0) {
              string[] nums = oldVersion.Split(‘.‘);
            if (nums.Length >= incParamNum && nums[incParamNum - 1] != "*") {
            Int64 val = Int64.Parse(nums[incParamNum - 1]);
            val++;
            nums[incParamNum - 1] = val.ToString();
            newVersion = nums[0];
            for (int i = 1; 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;
    }
     }
}

预先生成事件命令行:

if ‘$(ConfigurationName)‘==‘Release‘ goto :updateVersion

:updateVersion
call $(SolutionDir)AssemblyInfoUtil.exe -inc:4 $(ProjectDir)Properties\AssemblyInfo.cs
:exit

时间: 2024-08-19 19:55:25

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

编译一次自动更新版本号

http://blog.csdn.net/gqqnb/article/details/7213611 安装与运行 复制IncBuildNo.exe到你的硬盘,例如 C:\windows\IncBuildNo.exe      在Visual Studio中打开任意一个项目或解决方案, 在菜单上选择 项目 - >XXX属性- >生成事件 在后期生成事件命令行中输入命令:IncBuildNo "$(ProjectDir)Properties\AssemblyInfo.cs" &

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

在做界面程序时,常常需要一些数据类,界面元素通过绑定等方式显示出数据,然而由于UI线程不是线程安全的,一般都需要通过Invoke等方式来调用界面控件.但对于数据绑定bindingList而言,没法响应listchang事件,导致后端的grid等控件不能更新数据.废了好大的劲终于找到一个UIBindingList,实现线程数据的同步! using System; using System.ComponentModel; using System.Threading; using System.Wi

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

不显示带星号的第一列: datagridview属性框中将 RowHeadersVisiber 设置为  false 不显示未绑定列: datagridview有一个属性是 AutoGenerateColumns.但是让人不理解的是这个属性不在属性框中,因此你在那里找不到它.只能在后台代码中进行设置. this.datagridview1.AutoGenerateColumns  = false; 数据源发生改变时自动更新: datagridview的数据源在页面操作的过程中经常会发生变化.我们

在项目生成时就合并压缩你的js

对网站优化来讲,合并压缩js.css等静态内容是必修课之一,一则可以节省宽带:二则可以减少http请求:三则加快了网站的访问速度 对于如何实现合并压缩js 方案一: 继承IHttpModule ,对http进行拦截,然后获取其<script src=*> tag,将js文件取出来合并成为一个资源文件 方案二:如果你用的web解决方案为 asp.net mvc 4,那么当然可以用Bundle技术. 方案三:不依赖任何服务器技术,本地先合并压缩好后再上传 本文着重讲解方案三 要合并文件,可以用do

自动更新 -- 版本号比较(2)

版本号比较 在实现自动更新的时候,需要进行版本号的比较. 例如: 1.0.6 和1.0.7比较大小 解决方案: 将版本号的字符串转换成整数来比较. 步骤: 1. 去除字符串中的小数点.(使用正则表达式) 2. 比较两个字符串的长度,长度短的后尾补0,直到两个字符串长度相同.(为了实现 1.0.5.1与1.0.6的比较). 3. 将两个字符串转换成整数比较. 代码如下: // 正则表达式去除小数点 //String str1 = "1.0.6".replaceAll("[.]&

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

解决方案:VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK1123” 错误,解决方案为将 项目|项目属性|配置属性|清单工具|输入和输出|嵌入清单 “是”改为“否”即可,但是没新建一个项目都要这样设置一次. 在建立VS2010 Win32 Project项目时,按照上面解决方案依然发生了“error LNK1123”错误,经过上网查资料,解决方案为: 第一步:与上相同. 第二步:将 项目|项目属性|配置属性|连接器|清单文件|嵌入清单 “是”

java web项目启动时自动加载自定义properties文件

首先创建一个类 public class ContextInitListener implements ServletContextListener 使得该类成为一个监听器.用于监听整个容器生命周期的,主要是初始化和销毁的. 类创建后要在web.xml配置文件中增加一个简单的监听器配置,即刚才我们定义的类. Xml代码 <listener> <!-- lang: xml --> <description>ServletContextListener</descri

如何实现插入数据时自动更新另外一个表的内容

1事务:在程序中,将插入代码和更新代码包裹在一个事务里,失败后回滚,保证同时成功同时失败. 2存储过程:在数据库写存储过程,存储过程里面代码包裹在事务里,失败后回滚. 3触发器: SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER TRIGGER [dbo].[tri_test] ON [dbo].[销售情况信息] AFTER INSERT AS BEGIN UPDATE dbo.库存基本信息 SET 商品现存数量 = 商品现存数量 -

设置EntityFramework 在开发时自动更新数据库

1. NuGet 下载EntityFramework. 2. 定义Context 和 打开NuGet 命令 执行 Enable-Migrations , Libaray.DAL.Migrations.Configuration 要在执行命令后自动产生. using Libaray.Models.Entities; using Libaray.Models.Maps; using System; using System.Collections.Generic; using System.Data