zoukankan      html  css  js  c++  java
  • .net 项目生成时自动更新版本号

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

    Examples

    AssemblyInfoUtil.exe -set:3.1.7.53 "C:Program FilesMyProject1AssemblyInfo.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;
        }
         }
    }

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

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


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

    Automatic Versions

  • 相关阅读:
    ASP.NET MVC案例——————拦截器
    Windows Azure Virtual Network (10) 使用Azure Access Control List(ACL)设置客户端访问权限
    Windows Azure Storage (20) 使用Azure File实现共享文件夹
    Windows Azure HandBook (5) Azure混合云解决方案
    Windows Azure Service Bus (6) 中继(Relay On) 使用VS2013开发Service Bus Relay On
    Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP
    Windows Azure Service Bus (5) 主题(Topic) 使用VS2013开发Service Bus Topic
    Azure China (9) 在Azure China配置CDN服务
    Windows Azure Storage (19) 再谈Azure Block Blob和Page Blob
    Windows Azure HandBook (4) 分析Windows Azure如何处理Session
  • 原文地址:https://www.cnblogs.com/guqiangjs/p/6222652.html
Copyright © 2011-2022 走看看