Technology Toolbox

Your technology Sherpa for the Microsoft platform

Jeremy Jameson - Founder and Principal



Incrementing the Assembly Version for Each Build

This post originally appeared on my MSDN blog:

Since I no longer work for Microsoft, I have copied it here in case that blog ever goes away.

Last summer I wrote a post about best practices for .NET assembly versioning and made the following statement:

The AssemblyFileVersionAttribute should be incremented automatically as part of the build process.

In the comments for that post, someone asked exactly what I meant by that -- specifically if I used something in the pre-build event to increment the assembly version.

Here was my response:

While you certainly *could* increment the AssemblyFileVersionAttribute in a pre-build event, I definitely don't recommend it. Doing so would cause the version to increment each and every time *any* member of the Development team builds the solution.

I suppose you could simply tell developers not to check-in the updated AssemblyVersionInfo.cs file, but there are definitely better ways to accomplish the desired outcome.

Rather, I recommend incrementing the AssemblyFileVersionAttribute as part of your automated build process. In other words, each time an "official" build is created on the Build Server, the AssemblyVersionInfo.cs file is automatically checked out from source control, incremented, and checked back in.

Obviously, the actual implementation of this process will vary depending on your particular toolset. For example, if you are using Team Foundation Server, you can setup a custom task that increments the AssemblyFileVersionAttribute as part of the build. Several people have already blogged about the details of this for TFS. If you just bing "TFS increment build" you should get some good hits within the first page of search results. In particular, make sure you read Buck Hodges blog entry if you are using continuous integration.

Well, I probably should have blogged long ago about the specific process that I use for incrementing the assembly version, but you know what they say: "better late than never" ;-)

Note that this implementation has some specifics to Team Foundation Server, but I imagine you could tweak this fairly easily if you are using some other configuration management system and build process.

Update (2010-11-29)

This post was originally created for TFS 2005/2008. Refer to the following if you are using TFS 2010:

Unfortunately, there's no out-of-the-box task in the current version of MSBuild that increments an assembly version. However, you can write your own with just a few lines of code (there are a number of samples out there if you search for them), or -- as I prefer -- you can just use the one from the MSBuild Community Tasks Project. [There are quite a few other custom tasks in this package that you may find useful in addition to the Version task that I cover in this post.]

After downloading and installing the custom tasks, import them into your TFSBuild.proj file by adding the following line just below the <Project> element:

  <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

Note that there may be times when we want to build the solution without incrementing the assembly version; for example, to test changes to TFSBuild.proj on a local development environment (as opposed to checking in the changes and "testing" the changes on the build server). Therefore, let's allow a custom property to be specified that will skip the process of incrementing the assembly version. Inside the <PropertyGroup> element, add the following:

    <!-- Set this property to true to build without incrementing the assembly version. -->
      Condition=" '$(SkipIncrementAssemblyVersion)' == '' " >false</SkipIncrementAssemblyVersion>

Here is an example of specifying this property as a command-line option:

msbuild TFSBuild.proj /property:SkipIncrementAssemblyVersion=true

Next, add a property so that we can use the TFS command-line utility to checkout the assembly version files and subsequently check them back in:

Update (2010-05-05)

Note that the path to the TFS command-line utility has changed for a TFS 2010 build server.

To use the same technique on a TFS 2010 build server, specify the following instead:


One of the things that I've struggled with in the past is that "Desktop Builds" and "Team Builds" behave quite differently in certain areas. For example, when performing a Desktop Build (i.e. running msbuild on TFSBuild.proj from a command prompt), the SolutionRoot and BuildProjectFolderPath properties for my sample Fabrikam solution are set as follows:

  • SolutionRoot: C:\NotBackedUp\Fabrikam\Demo
  • BuildProjectFolderPath: C:\NotBackedUp\Fabrikam\Demo\Main\Source

However, when performing a Team Build (i.e. when a build is queued or scheduled on a build server), the properties are set as follows:

  • SolutionRoot: C:\Users\svc-build\AppData\Local\Temp\Demo\Automated Build - Main\Sources
  • BuildProjectFolderPath: $/Demo/Main/Source

Consequently, define a new property (SolutionWorkingDirectory) and condititionally set it to the expected location:

  <!-- HACK: The values of $(SolutionRoot) and $(BuildProjectFolderPath) vary
  significantly between desktop builds and builds started using Team
  Foundation Build (in other words, builds queued on TFS build agents).
  For desktop builds:
  SolutionRoot = C:\NotBackedUp\Fabrikam\Demo
  BuildProjectFolderPath = C:\NotBackedUp\Fabrikam\Demo\Main\Source
  For builds performed using Team Foundation Build:
  SolutionRoot = C:\Users\svc-build\AppData\Local\Temp\Demo\Automated Build - Main\Sources
  BuildProjectFolderPath = $/Demo/Main/Source
  In order to update files (i.e. check out and subseqeuntly check-in) in the solution
  regardless of which build type is currently running, conditionally  set the
  "SolutionWorkingDirectory" property.
  <PropertyGroup Condition=" '$(IsDesktopBuild)' == 'true' ">
  <PropertyGroup Condition=" '$(IsDesktopBuild)' != 'true' ">

Next, override the AfterGet target to checkout the version files from TFS, and subsequently update the assembly version:

  <Target Name="AfterGet">
    <CallTarget Targets="CheckOutVersionFilesFromSourceControl"/>
    <CallTarget Targets="UpdateVersionFilesInSourceControl"/>

Here is the custom target to checkout the version files. Note that this target is skipped if the SkipIncrementAssemblyVersion property is set to true.

  <Target Name="CheckOutVersionFilesFromSourceControl"
    Condition=" '$(SkipIncrementAssemblyVersion)' != 'true' ">
    <Message Importance="high"
      Text="Checking out version files from source control..." />

    <Message Importance="high"
      Text="SolutionWorkingDirectory: $(SolutionWorkingDirectory)" />

      Command="$(TeamFoundationVersionControlTool) checkout AssemblyVersionInfo.txt AssemblyVersionInfo.cs"/>


Note that AssemblyVersionInfo.txt is just a simple text file used by the custom Version task from the MSBuild Community Tasks Project and only contains a simple version string. For example:

The AssemblyVersionInfo.cs file is generated by the custom Version task. For example:

// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4200
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>

using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyFileVersion("")]

Here is the custom target to update the version files in TFS. Like the CheckOutVersionFilesFromSourceControl target, the UpdateVersionFilesInSourceControl target is skipped if the SkipIncrementAssemblyVersion property is set to true.

  <Target Name="UpdateVersionFilesInSourceControl"
    Condition=" '$(SkipIncrementAssemblyVersion)' != 'true' ">
    <Message Importance="high"
      Text="Updating version files in source control..." />

    <Message Importance="high"
      Text="SolutionWorkingDirectory: $(SolutionWorkingDirectory)" />

      Condition=" '$(SkipIncrementAssemblyVersion)' != 'true' and '$(IsDesktopBuild)' != 'true' "

      Condition=" '$(SkipIncrementAssemblyVersion)' != 'true' "
      AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />

      Command="$(TeamFoundationVersionControlTool) checkin
 /override:&quot;Check-in from automated build&quot;
 /comment:&quot;Increment assembly version ($(BuildNumber)) $(NoCICheckinComment)&quot;
 AssemblyVersionInfo.txt AssemblyVersionInfo.cs"/>

Note that for a Team Build, we need to copy the AssemblyVersionInfo.txt file from the BuildType folder into the solution folder. This allows the same process to be used for Desktop Builds and Team Builds. Also note that $(NoCICheckinComment) is specified when checking in the files from the build (as I mentioned in my earlier comment, see Buck Hodges' blog post for more details on this).

Finally, use the BuildNumberOverrideTarget and a custom target to actually increment the assembly version using the custom Version task and set the BuildNumber property accordingly. Note that if SkipIncrementAssemblyVersion is set to true, the assembly version is not incremented and the BuildNumber property is set to whatever is currently specified in AssemblyVersionInfo.txt.

  <Target Name="BuildNumberOverrideTarget">
    <CallTarget Targets="SetBuildNumber"/>

  <Target Name="SetBuildNumber">
    <Attrib Files="AssemblyVersionInfo.txt" ReadOnly="false"/>

      Condition=" '$(SkipIncrementAssemblyVersion)' == 'true' "
      <Output TaskParameter="Major" PropertyName="Major" />
      <Output TaskParameter="Minor" PropertyName="Minor" />
      <Output TaskParameter="Build" PropertyName="Build" />
      <Output TaskParameter="Revision" PropertyName="Revision" />

      Condition=" '$(SkipIncrementAssemblyVersion)' != 'true' "
      <Output TaskParameter="Major" PropertyName="Major" />
      <Output TaskParameter="Minor" PropertyName="Minor" />
      <Output TaskParameter="Build" PropertyName="Build" />
      <Output TaskParameter="Revision" PropertyName="Revision" />

    <Message Importance="high"
      Text="Build number set to &quot;$(BuildNumber)&quot;" />

Note that in the example above, the BuildType attribute on the <Version> element is set to "Increment" when the assembly version should be incremented (thus generating build numbers like,, etc.). This is what I recommend for the Main branch.

For the QFE branch, I recommend changing the BuildType attribute to "None" and the RevisionType attribute to "Increment" (to generate build numbers like,, etc.).

Refer to one of my previous posts more information on shared assembly files in Visual Studio projects.


  1. # re: Incrementing the Assembly Version for Each Build

    August 15, 2010 8:12 PM


    Since we are using TFS 2010, what is the preferred way to perform the mentioned steps?

    We don't have a tfsbuild.proj...

    Thanks for help,

      Günther Humer

  2. # re: Incrementing the Assembly Version for Each Build

    September 1, 2010 5:32 PM

    Same question here - how do you do this in 2010 where there is no TfsBuild.proj file?


  3. # i want to place date,month in build number and year in revision number. how to do this?

    November 21, 2011 11:56 AM
    Hi i am working on assembly number. i got struck between.
    i want to place the month,day in place of build number and
    year on revision number, whenever i build the project. tell me how can i do this?
  4. # re: Incrementing the Assembly Version for Each Build

    November 21, 2011 9:25 PM
    Jeremy Jameson

    If you want to use a date-based scheme for assembly versions instead of the scheme I use, then refer to the approach used by John Robbins over at Wintellect (just Google "Robbins assembly version" and you should find it quite easily).
  5. # re: Incrementing the Assembly Version for Each Build

    May 21, 2014 2:53 PM
    Thirded the comment about using TFS 2010

Add Comment

Optional, but recommended (especially if you have a Gravatar). Note that your email address will not appear with your comment.
If URL is specified, it will be included as a link with your name.

To prevent spam from being submitted, please select the following fruit: Strawberry

Please add 8 and 8 and type the answer here: