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

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

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

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

有帮助吗?

解决方案

对于“内置”内容,您不能这样做,因为使用 1.0.* 或 1.0.0.* 将用编码日期/时间戳替换修订版和内部版本号,这通常也是一个好方法。

欲了解更多信息,请参阅 汇编链接器 /v 标记中的文档。

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

装配信息任务

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

有 2 个陷阱:

  1. 版本字符串中的 4 个数字中的每一个数字都限制为 65535。这是 Windows 的限制,不太可能得到修复。
  2. 与 Subversion 一起使用需要进行一些小的更改:

检索版本号非常容易:

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。

其他提示

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

程序集版本位于 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")> 

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

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

您使用什么源代码控制系统?

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

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

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

[Visual Studio 2017, .csproj 特性]

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

我手动更新主次编号,但让 MSBuild 自动更新内部版本号(1.1.1)。1, 1.1.2, 1.1.3, , ETC。:)

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 文件中添加下一个代码:

<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 API 密钥 nuget SetApiKey <my-api-key> 或者在 NuGet 推送调用中包含该密钥)。

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

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

  1. 将装配信息分为产品部分(公司名称、版本等)和装配特定部分(装配名称等)。看 这里
  2. 另外 - 我使用 subversion,所以我发现将内部版本号设置为 subversion 修订号很有帮助,从而使始终轻松返回生成程序集的代码库(例如1.4.100.1502 是根据修订版 1502 构建的)。

如果您想要一个在每次编译完成时更新的自动递增数字,您可以使用 版本更新器 来自预构建事件。如果您愿意,您的预构建事件可以检查构建配置,以便版本号只会针对发布构建而增加(例如)。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top