关于c#:自动更新版本号

关于c#:自动更新版本号

Automatically update version number

我希望每个构建版本都可以增加应用程序的version属性,但是我不确定如何在Visual Studio(2005/2008)中启用此功能。 我尝试将AssemblyVersion指定为1.0。*,但它并不能完全满足我的需求。

我还在使用设置文件,并且在较早的尝试中,当程序集版本更改时,由于应用程序在另一个目录中查找设置文件,因此我的设置被重置为默认设置。

我希望能够以1.1.38的形式显示版本号,以便当用户发现问题时,我可以记录他们正在使用的版本,并告诉他们如果具有旧版本,则进行升级。

对版本控制如何工作的简短解释也将不胜感激。 版本号和修订号何时增加?


使用"内置"的东西,您将无法做到,因为使用1.0。*或1.0.0。*会用编码的日期/时间戳替换修订版和内部版本号,这通常也是一种好方法。

有关更多信息,请参见/ v标记中的Assembly Linker文档。

至于自动递增数字,请使用AssemblyInfo任务:

AssemblyInfo任务

可以将其配置为自动增加内部版本号。

有2个陷阱:

  • 版本字符串中的4个数字中的每个数字都限制为65535。这是Windows的限制,不太可能得到解决。

    • 为什么内部版本号限制为65535?
  • 与Subversion一起使用时需要进行一些小的更改:

    • 使用MSBuild在生成时生成程序集版本信息(包括SubVersion修复)
  • 这样,获取版本号就很容易了:

    1
    2
    Version v = Assembly.GetExecutingAssembly().GetName().Version;
    string About = string.Format(CultureInfo.InvariantCulture, @"YourApp Version {0}.{1}.{2} (r{3})", v.Major, v.Minor, v.Build, v.Revision);

    并且要澄清一下:在.net或至少在C#中,该内部版本实际上是THIRD号,而不是某些人(例如习惯于Major.Minor.Release.Build的Delphi开发人员)所期望的第四个数字。

    在.net中,它是Major.Minor.Build.Revision。


    VS.NET将Assembly版本默认为1.0。*,并在自动递增时使用以下逻辑:它将构建部分设置为自2000年1月1日以来的天数,并将修订部分设置为自午夜以来的秒数,当地时间除以2。请参阅此MSDN文章。

    程序集版本位于assemblyinfo.vb或assemblyinfo.cs文件中。从文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    ' Version information for an assembly consists of the following four values:
    '

    '      Major Version
    '
         Minor Version
    '      Build Number
    '
         Revision
    '
    '
    You can specify all the values or you can default the Build and Revision Numbers
    ' by using the '*' as shown below:
    '
    <Assembly: AssemblyVersion("1.0.*")>

    <Assembly: AssemblyVersion("1.0.0.0")>
    <Assembly: AssemblyFileVersion("1.0.0.0")>


    我发现,只要需要产品版本,就可以使用以下命令简单显示上次构建的日期:

    1
    System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString("yyyy.MM.dd.HH.mm.ss")

    而不是尝试从类似以下内容的版本:

    1
    2
    3
    4
    5
    6
    7
    8
    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
    object[] attributes = assembly.GetCustomAttributes(typeof(System.Reflection.AssemblyFileVersionAttribute), false);
    object attribute = null;

    if (attributes.Length > 0)
    {
        attribute = attributes[0] as System.Reflection.AssemblyFileVersionAttribute;
    }

    您正在使用哪种源代码控制系统?

    几乎所有文件都具有某种形式的$ Id $标签,该文件会在签入文件时扩展。

    我通常使用某种形式的骇客将其显示为版本号。

    另一种选择是使用日期作为内部版本号:080803-1448


    [Visual Studio 2017,.csproj属性]

    要自动更新PackageVersion / Version / AssemblyVersion属性(或其他任何属性),首先,创建一个新的Microsoft.Build.Utilities.Task类,该类将获取您的当前内部版本号并发送回更新的版本号(我建议为此创建一个单独的项目类)。

    我手动更新了major.minor号,但让MSBuild自动更新了内部版本号(1.1.1、1.1.2、1.1.3等:)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    using Microsoft.Build.Framework;
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class RefreshVersion : Microsoft.Build.Utilities.Task
    {
        [Output]
        public string NewVersionString { get; set; }
        public string CurrentVersionString { get; set; }

        public override bool Execute()
        {      
            Version currentVersion = new Version(CurrentVersionString ??"1.0.0");

            DateTime d = DateTime.Now;
            NewVersionString = new Version(currentVersion.Major,
                currentVersion.Minor, currentVersion.Build+1).ToString();
            return true;
        }

    }

    然后调用您最近在MSBuild上创建的任务,在.csproj文件上添加下一个代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <Project Sdk="Microsoft.NET.Sdk">    
    ...
    <UsingTask TaskName="RefreshVersion" AssemblyFile="$(MSBuildThisFileFullPath)\..\..\<dll path>\BuildTasks.dll" />
    <Target Name="RefreshVersionBuildTask" BeforeTargets="Pack" Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
       <RefreshVersion CurrentVersionString="$(PackageVersion)">
              <Output TaskParameter="NewVersionString" PropertyName="NewVersionString" />            
       </RefreshVersion>
       <Message Text="Updating package version number to $(NewVersionString)..." Importance="high" />
       <XmlPoke XmlInputPath="$(MSBuildProjectDirectory)\mustache.website.sdk.dotNET.csproj" Query="/Project/PropertyGroup/PackageVersion" Value="$(NewVersionString)" />
    </Target>
    ...
    <PropertyGroup>
     ..
     <PackageVersion>1.1.4</PackageVersion>
     ..

    选择Visual Studio Pack项目选项(在构建之前只需更改为BeforeTargets="Build"以执行任务)时,将触发RefreshVersion代码以计算新版本号,并且XmlPoke任务将相应地更新您的.csproj属性(是的,将修改文件)。

    在使用NuGet库时,我还通过将下一个构建任务添加到上一个示例中,将该包发送到NuGet存储库。

    1
    2
    3
    4
    5
    <Message Text="Uploading package to NuGet..." Importance="high" />
    <Exec WorkingDirectory="$(MSBuildProjectDirectory)\bin
    elease"
    Command="c:
    uget
    uget push *.nupkg -Source https://www.nuget.org/api/v2/package"
    IgnoreExitCode="true" />

    c:
    uget
    uget
    是我拥有NuGet客户端的位置(请记住要通过调用nuget SetApiKey 来保存您的NuGet API密钥,或者将该密钥包括在NuGet推送调用中)。

    以防万一它可以帮助某人^ _ ^。


    不久前,我写了一个快速而肮脏的exe文件,该文件将更新assemblyinfo中的版本号。{cs / vb}-我还使用过rxfind.exe(一种简单且功能强大的基于正则表达式的搜索替换工具)来执行在构建过程中从命令行进行更新。其他一些有用的提示:

  • 将assemblyinfo分为产品零件(公司名称,版本等)和特定于装配的零件(装配名称等)。看这里
  • 另外-我使用Subversion,因此我发现将内部版本号设置为Subversion修订版本号很有帮助,因此可以很容易地始终返回到生成程序集的代码库(例如1.4.100.1502是从1502版本构建的)。

  • 如果您希望每次编译时都会更新一个自动递增的数字,则可以使用预构建事件中的VersionUpdater。如果您愿意,您的预构建事件可以检查构建配置,以便版本号仅针对Release版本(例如)递增。


    推荐阅读