Auto-increment build numbers in Visual Studio

You need to auto-increment your build numbers in order to easily tell which code you’re working with.

In this article I’ll explain how to auto-increment your build numbers in Visual Studio. I’ll be using text templating to generate the Assembly Version.

1 – Choose a versioning scheme

I’m going to be using the version scheme: <Major Version>.<Minor Version>.<Days Since Project Started>.<Minutes Since Midnight>. You should use whatever makes sense for you.

The one rule you must adhere to is the numbers must be <= 65534 (because they are 16-bit). If you generate a number greater than 65534, you’ll get a build error:

CS7034  The specified version string does not conform to the required format - major[.minor[.build[.revision]]] 

There are 86400 seconds per day and 1440 minutes per day. This is why I chose Minutes Since Midnight instead of Seconds Since Midnight. Since 86400 > the limit of 65534, using seconds would sometimes result in the build error shown above. By using minutes, this cannot happen.

2 – Comment out the assembly version properties

Open AssemblyInfo.cs and comment out AssemblyVersion and AssemblyFileVersion.

3 – Add a Text Template file

A Text Template is used to generate code. We’ll be using this to generate the assembly version.

After you add this file you’ll get a warning prompt. Since you’re the one adding this file, you can check the box and click OK.

4 – Update the Text Template to generate the AssemblyVersion property

There’s two parts to the text template:

  1. Specifying the template using placeholder variables.
  2. Populating the placeholder variables.
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>

using System.Reflection;

[assembly: AssemblyVersion("<#= this.Major #>.<#= this.Minor #>.<#= this.DaysSinceProjectStarted #>.<#= this.MinutesSinceMidnight  #>")]

<#+

 int Major = 1;
 int Minor = 0;
 
 static DateTime ProjectStartedDate = new DateTime(year: 2020, month: 3, day: 12);
 int DaysSinceProjectStarted = (int)((DateTime.UtcNow - ProjectStartedDate).TotalDays);

 int MinutesSinceMidnight = (int)DateTime.UtcNow.TimeOfDay.TotalMinutes;

#>

5 – Update .csproj to run the text template every time it builds

  • Open your .csproj file in notepad
  • Add TextTemplating.targets after Microsoft.CSharp.targets (if you’re using C#):
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v16.0\TextTemplating\Microsoft.TextTemplating.targets" />

Note: This is specifically adding the VS2019 path.

  • In the appropriate PropertyGroup(s), for example, the Debug build configuration, add the following properties:
<TransformOnBuild>true</TransformOnBuild>
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>

I added these properties to both Debug/Release build configurations. In the end, your .csproj should have the following properties:

.csproj - enable text templating auto-run on build properties

6 – Build the project

When you build the project it’ll execute the text template. This generates a source file with the AssemblyVersion property.

2 thoughts on “Auto-increment build numbers in Visual Studio”

  1. To me, this is totally not working!
    Yes, the VersionAutoIncrementer.cs file is created, but it is stuck in its original version number.
    E.g. stuck in:

    using System.Reflection;
    [assembly: AssemblyVersion(“8.0.220.927”)]

    Reply
    • Hi Bjorn,

      Good catch. I updated the page to include instructions for how to update your .csproj file to make it run the text templating transform on every build.

      Please take a look at the step I added to the page – 5 – Update .csproj to run the text template every time it builds.

      Let me know if this helps.

      Reply

Leave a Comment