C# and Auto-Incrementing Build Numbers in VS 2013

Anyone used to VB6 will remember that it can be configured to automatically increment the build number each time the code is built.  It was a simple way to track versions.For a C# project I have been working on I was looking for a way to re-implement this in C#.  With a degree of trial and error I found a way to do this - there are some internet references out there that are somewhat problematic, so hopefully this may save some time for a reader.
The default AssemblyInfo.cs file has these commented lines:

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//

Beneath these lines are two further lines:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

You can use a documented technique to get these to increment, simply replace these two lines with

[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]

You can now build the assembly and you will see something like this in the assembly properties details tab: 



These numbers are created automagically but they have the following roots:
Build Number=5591 ==>the number of days since 1st January 2000
Revision=967 ==>????

 
According to usually well informed sources 967 is the half of the number of seconds since midnight.  I am not convinced that this is a useful statement - midnight where?  My testing shows that this is "since midnight in the timezone set on your PC - but daylight saving time is ignored".  The build shown above was created at 32 minutes and 14 seconds past 11pm at night, but this timezone has moved to daylight saving time and the clocks have gone back, so the time used for the build was actually 32 minutes and 14 seconds past midnight.
 
So, how can we automate this process to an old fashioned "increment the build by one when you build"?
 
With Text Templates - a way to write code that writes code.  Just follow the steps below:
  • In the AssemblyInfo.cs file comment out these two lines:
//[assembly: AssemblyVersion("1.0.0.0")]
//[assembly: AssemblyFileVersion("1.0.0.0")]
  • In your project create a new file called AFV.tt - a text template file.
  • Cut and paste these lines into the AFV.tt file
<#@ template language="C#" hostSpecific="True"#>
<#@ output extension="cs" #>


<#@ import namespace="System.IO" #>


<#


int buildnumber;


 
try
{
using(var f = File.OpenText(this.Host.ResolvePath("AFV.cs")))
{
string s = f.ReadLine().Replace("//","");
buildnumber = int.Parse(s) + 1;
}
}catch
{    
buildnumber = 0;
}

#>
//<#=buildnumber#>
//
//

using System.Reflection;


[assembly: AssemblyFileVersion("1.0.<#= buildnumber #>.0")]
 
Now in the project properties set up a pre-build event:

For clarity, the text in the event is shown below:

"c:\Program Files (x86)\Common Files\Microsoft Shared\TextTemplating\$(VisualStudioVersion)\texttransform.exe" "$(ProjectDir)\Classes\AFV.tt"

This ensures that each time you build the assembly the build number will increment by one. To make this even easier for the reader, I will shortly publish this code to github:
https://github.com/JohnGreenan/FinancialServerSolace  

Comments