自动更新版本号

Rob*_*und 106 c# versioning visual-studio

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

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

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

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

Mic*_*tum 96

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

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

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

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);
Run Code Online (Sandbox Code Playgroud)

并且,澄清:在.net或至少在C#中,构建实际上是第三个,而不是第四个,因为有些人(例如习惯于Major.Minor.Release.Build的Delphi开发人员)可能期望.

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

  • 这是否意味着在2179年6月4日,Microsoft默认版本号将被破坏?(2000年后的第65536天) (6认同)
  • 我刚刚发现这个类似的Visual Studio插件:http://autobuildversion.codeplex.com/ (3认同)
  • @SanuelJackson哈哈!是的,它会.太糟糕了,我跟不上2010年的评论,对不起!:P时间和版本的进程令我们所有人感到悲伤. (2认同)

Sol*_*pod 22

VS.NET默认将Assembly版本设置为1.0.*并在自动递增时使用以下逻辑:它将构建部分设置为自2000年1月1日以来的天数,并将修订部分设置为自午夜以来的秒数,当地时间,除以2.请参阅此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")> 
Run Code Online (Sandbox Code Playgroud)


use*_*167 10

我发现只需在需要产品版本的地方使用以下内容简单地显示上次构建的日期就可以了:

System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString("yyyy.MM.dd.HH.mm.ss")
Run Code Online (Sandbox Code Playgroud)

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

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 我想你的意思是:yyyy.MM.dd.HHmm不是yyyy.MM.dd.HHMM. (6认同)

eng*_*ech 6

你使用什么源控制系统?

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

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

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


Nac*_*oll 6

[Visual Studio 2017,.csproj属性]

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

我手动更新主次编号,但让 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后调用最近在 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>
 ..
Run Code Online (Sandbox Code Playgroud)

当选择 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" />
Run Code Online (Sandbox Code Playgroud)

c:\nuget\nuget是我拥有 NuGet 客户端的位置(请记住通过调用保存 NuGet API 密钥nuget SetApiKey <my-api-key>或在 NuGet 推送调用中包含该密钥)。

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