c# - Versionsnummer automatisch aktualisieren

original title: "c# - Automatically update version number"


I would like the version property of my application to be incremented for each build but I'm not sure on how to enable this functionality in Visual Studio (2005/2008). I have tried to specify the AssemblyVersion as 1.0.* but it doesn't get me exactly what I want.

I'm also using a settings file and in earlier attempts when the assembly version changed my settings got reset to the default since the application looked for the settings file in another directory.

I would like to be able to display a version number in the form of 1.1.38 so when a user finds a problem I can log the version they are using as well as tell them to upgrade if they have an old release.

A short explanation of how the versioning works would also be appreciated. When does the build and revision number get incremented?

Ich möchte, dass die Versionseigenschaft meiner Anwendung für jeden Build erhöht wird, bin mir jedoch nicht sicher, wie diese Funktionalität in Visual Studio (2005/2008) aktiviert werden soll. Ich habe versucht, die ...

Dies ist die Zusammenfassung nach der Übersetzung. Wenn Sie die vollständige Übersetzung anzeigen möchten, klicken Sie auf das Symbol "Übersetzen"

Alle Antworten
  • Translate

    With the "Built in" stuff, you can't, as using 1.0.* or 1.0.0.* will replace the revision and build numbers with a coded date/timestamp, which is usually also a good way.

    For more info, see the Assembly Linker Documentation in the /v tag.

    As for automatically incrementing numbers, use the AssemblyInfo Task:

    AssemblyInfo Task

    This can be configured to automatically increment the build number.

    There are 2 Gotchas:

    1. Each of the 4 numbers in the Version string is limited to 65535. This is a Windows Limitation and unlikely to get fixed.
    2. Using with with Subversion requires a small change:

    Retrieving the Version number is then quite easy:

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

    And, to clarify: In .net or at least in C#, the build is actually the THIRD number, not the fourth one as some people (for example Delphi Developers who are used to Major.Minor.Release.Build) might expect.

    In .net, it's Major.Minor.Build.Revision.

  • Translate

    VS.NET defaults the Assembly version to 1.0.* and uses the following logic when auto-incrementing: it sets the build part to the number of days since January 1st, 2000, and sets the revision part to the number of seconds since midnight, local time, divided by two. See this MSDN article.

    Assembly version is located in an assemblyinfo.vb or assemblyinfo.cs file. From the file:

    ' 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("")> 
    <Assembly: AssemblyFileVersion("")> 

  • Translate

    I have found that it works well to simply display the date of the last build using the following wherever a product version is needed:


    Rather than attempting to get the version from something like the following:

    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;

  • Translate

    What source control system are you using?

    Almost all of them have some form of $ Id $ tag that gets expanded when the file is checked in.

    I usually use some form of hackery to display this as the version number.

    The other alternative is use to use the date as the build number: 080803-1448

  • Translate

    [Visual Studio 2017, .csproj properties]

    To automatically update your PackageVersion/Version/AssemblyVersion property (or any other property), first, create a new Microsoft.Build.Utilities.Task class that will get your current build number and send back the updated number (I recommend to create a separate project just for that class).

    I manually update the major.minor numbers, but let MSBuild to automatically update the build number (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
        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;

    Then call your recently created Task on MSBuild process adding the next code on your .csproj file:

    <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" />             
       <Message Text="Updating package version number to $(NewVersionString)..." Importance="high" />
       <XmlPoke XmlInputPath="$(MSBuildProjectDirectory)\mustache.website.sdk.dotNET.csproj" Query="/Project/PropertyGroup/PackageVersion" Value="$(NewVersionString)" />

    When picking Visual Studio Pack project option (just change to BeforeTargets="Build" for executing the task before Build) the RefreshVersion code will be triggered to calculate the new version number, and XmlPoke task will update your .csproj property accordingly (yes, it will modify the file).

    When working with NuGet libraries, I also send the package to NuGet repository by just adding the next build task to the previous example.

    <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 is where I have the NuGet client (remember to save your NuGet API key by calling nuget SetApiKey <my-api-key> or to include the key on the NuGet push call).

    Just in case it helps someone ^_^.

  • Translate

    Some time ago I wrote a quick and dirty exe that would update the version #'s in an assemblyinfo.{cs/vb} - I also have used rxfind.exe (a simple and powerful regex-based search replace tool) to do the update from a command line as part of the build process. A couple of other helpfule hints:

    1. separate the assemblyinfo into product parts (company name, version, etc.) and assembly specific parts (assembly name etc.). See here
    2. Also - i use subversion, so I found it helpful to set the build number to subversion revision number thereby making it really easy to always get back to the codebase that generated the assembly (e.g. was built from revision 1502).

  • Translate

    If you want an auto incrementing number that updates each time a compilation is done, you can use VersionUpdater from a pre-build event. Your pre-build event can check the build configuration if you prefer so that the version number will only increment for a Release build (for example).