首页 文章

自动更新版本号

提问于
浏览
98

我希望我的应用程序的version属性可以为每个构建增加,但我不确定如何在Visual Studio(2005/2008)中启用此功能 . 我试图将AssemblyVersion指定为1.0 . *但它并没有让我得到我想要的 .

我也使用了一个设置文件,在早期的尝试中,当程序集版本发生变化时,我的设置被重置为默认设置,因为应用程序在另一个目录中查找了设置文件 .

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

还将理解版本控制如何工作的简短说明 . 构建和修订号何时增加?

7 回答

  • 6

    使用“内置”的东西,你不能,因为使用1.0 . *或1.0.0 . *将用编码的日期/时间戳替换修订和构建数字,这通常也是一个好方法 .

    有关详细信息,请参阅/ v标记中的Assembly Linker文档 .

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

    AssemblyInfo Task

    这可以配置为自动增加内部版本号 .

    有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#中,构建实际上是第三个,而不是第四个,因为有些人(例如习惯于Major.Minor.Release.Build的Delphi开发人员)可能会期望 .

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

  • 8

    如果需要每次编译完成时更新的自动递增数,可以使用预构建事件中的VersionUpdater . 如果您愿意,您的预构建事件可以检查构建配置,以便版本号仅为Release版本增加(例如) .

  • 19

    你使用什么源控制系统?

    几乎所有这些都有某种形式的$ Id $标签在签入文件时会被扩展 .

    我通常使用某种形式的hackery来显示它作为版本号 .

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

  • 0

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

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

    ' 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")
    

    而不是试图从以下的东西获取版本:

    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;
    }
    
  • 2

    [Visual Studio 2017, .csproj 属性]

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

    我手动更新major.minor数字,但让MSBuild自动更新内部版本号(1.1 . 1 ,1.1 . 2 ,1.1 . 3 等:)

    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进程上调用最近创建的Task,在.csproj文件中添加下一个代码:

    <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" 以在Build之前执行任务)时,将触发RefreshVersion代码以计算新版本号, XmlPoke 任务将相应地更新.csproj属性(是的,它将修改文件) ) .

    使用NuGet库时,我还将包发送到NuGet存储库,只需将下一个构建任务添加到上一个示例即可 .

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

    c:\nuget\nuget 是我拥有NuGet客户端的地方(记得通过调用 nuget SetApiKey <my-api-key> 保存NuGet API密钥或在NuGet推送调用中包含密钥) .

    以防它帮助某人^ _ ^ .

  • 89

    前段时间我写了一个快速而脏的exe文件,它将更新一个assemblyinfo中的版本# . {cs / vb} - 我也使用了rxfind.exe(一个简单而强大的基于正则表达式的搜索替换工具)来执行作为构建过程的一部分从命令行更新 . 其他一些帮助提示:

    • 将assemblyinfo分为产品部件(公司名称,版本等)和装配特定部件(装配件名称等) . 见here

    • 另外 - 我使用了subversion,所以我发现将构建号设置为subversion版本号很有帮助,这样可以很容易地总是回到生成程序集的代码库(例如1.4.100.1502是从版本1502构建的) .

相关问题