.net 項目產生時自動更新版本號碼

來源:互聯網
上載者:User

標籤:names   href   index   ...   info   visio   article   oid   ber   

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

 

.net 項目產生時自動更新版本號碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.