C# – Auto Increment Assembly Version Number

When it comes to developing software one thing that I find is always a pain is keeping track of and assigning version numbers. I’ve been writing a lot of stuff in C# lately and I’ve found a rather elegant way to achieve dynamic build numbers that automatically update each time you build the application.

In the .Net world each assembly receives its own version number of the form “a.b.c.d”, where each letter represents the major, minor, build, and revision number respectively. The goal here is to allow me to set the major and minor numbers manually, have the build number be the date stamp indicating the date the build took place, and have the revision number increment each build. The revision number should be reset each time the build number changes (i.e. the date of the build).

To accomplish this task we must call on Microsoft’s msbuild program. Msbuild is the build tool for .Net languages and is essentially like make from the Unix/Linux world. It turns out Microsoft has already created a task for msbuild to do what I need. It’s called “AssemblyInfo Task” and you can find it here.

In order to get the AssemblyInfo Task up and running first run the installer located in the download. When prompted install the task into the GAC. Next create a new project in Visual Studio. Copy the file

Microsoft.VersionNumber.targets from

“\Program Files\MSBuild\Microsoft\AssemblyInfoTask”

into your project directory.

Now, open your .csproj file in a text editor (it’s just an XML file). Look toward the end of the file and find the line

<Import Project=”$(MSBuildBinPath)\Microsoft.CSharp.targets” />.

Just below this line add:

<Import Project=”$(SolutionDir)\extra\tools\msbuild\Microsoft.VersionNumber.targets” />.

This will make sure the AssemblyInfo Task is run each time you build your project.

Finally rebuild your project. SUCCESS! No Wait. Not quite. You are probably looking at a warning such as: warning CS1607: Assembly generation — The version ’1.0.090331.00′ specified for the ‘file version’ is not in the normal ‘major.minor.build.revision’ format.

The problem is each of those fields in the version string are 16-bit fields meaning they can only contain values from 0 to 65,535. Since the date is later then May 2005 we have overflowed the build number. We can fix this though. Open the Microsoft.VersionNumber.targets file. Find the block of text that reads:

<PropertyGroup>
<AssemblyMajorVersion>1</AssemblyMajorVersion>
<AssemblyMinorVersion>0</AssemblyMinorVersion>
<AssemblyBuildNumber></AssemblyBuildNumber>
<AssemblyRevision></AssemblyRevision>
<AssemblyBuildNumberType>DateString</AssemblyBuildNumberType>
<AssemblyBuildNumberFormat>yyMMdd</AssemblyBuildNumberFormat>
<AssemblyRevisionType>AutoIncrement</AssemblyRevisionType>
<AssemblyRevisionFormat>00</AssemblyRevisionFormat>
</PropertyGroup>

This is what controls how the version number is generated. You can play with these options to get different results. For now, change the value inside the AssemblyBuildNumberFormat tag from yyMMdd to 01MMdd. Now we no longer have the year in our time stamp but we do have a valid version number with a constant “1” out front. You should make the same modification to the property group for Assembly File Version.

The last thing you need to do is reset the version numbers for your assembly. AssemblyTask needs to see the initial version as “1.0.0.0”. You can find these in the file AssemblyInfo.cs under the Properties folder in your solution. Change both Assembly Version and AssemblyFileVersion to “1.0.0.0”. Now reload you project. Viola! You now have auto incrementing assembly version numbers. The revision number will increment each time you rebuild and the build number will change each day.

This makes life much easier. I’ve put together a small demo application you can download that is already configured in the manner described above. Run the installer first for the AssemblyInfo Task then build and run the project. You can get the project HERE. You can also modify the major and minor build number and many other options within the file Microsoft.VersionNumber.targets.

Happy Coding!!!!!!!

April 1, 2009 · crow · 7 Comments
Posted in: Programming

7 Responses

  1. Legends - June 5, 2009

    Hi,

    I am using your sample project.
    I have added a new class lib project to the solution and modified it according your explanation above.
    It works!
    The assembly versions get updated during every build.

    But when I add the project output of the 2 projects to a setup project and try to build the setup, it fails:

    “General failure building folders”
    “Unrecoverable error”

    Do you know what’s wrong here?

    Kind regards,
    Legends

  2. Fakhrul’z » Blog Archive » .NET Auto Increment Version Number - July 30, 2009

    [...]  http://www.crowsprogramming.com/archives/27 [...]

  3. Jared - January 21, 2010

    For what its worth, there is a mismatch between the line and the actual line in the .csproj file.

    Also, another way to do this in VS 2008 is to change AssemblyInfo.cs and put * (asterisk) in the build number or revision fields, this will auto generated the build number.

    [assembly: AssemblyVersion("1.0.0.*")]
    [assembly: AssemblyFileVersion("1.0.0.*")]

    http://channel9.msdn.com/forums/TechOff/260670-VSnet-2008-Auto-Increment-BuildVersion-Numbers-how/

  4. glenneroo - February 23, 2010

    @Jared: This is how I generally have my versions configured, however, I always get this warning:

    Assembly generation — The version ’0.0.1.*’ specified for the ‘file version’ is not in the normal ‘major.minor.build.revision’ format

    Did you get around this somehow or do you just ignore it?

  5. pandora bracelets - November 9, 2010

    http://www.crowsprogramming.com/archives/40/comment-page-1#commentI have added a new class lib project to the solution and modified it according your explanation above.-18568

  6. Tweets that mention Crows Programming » C# – Auto Increment Assembly Version Number -- Topsy.com - December 4, 2010

    [...] This post was mentioned on Twitter by Tim Skauge, Tim Skauge. Tim Skauge said: Crows Programming » C# – Auto Increment Assembly Version Number http://bit.ly/83L2SV #bookmark [...]

  7. alan gilchrist - February 1, 2012

    Visitor recommendations…

    [...]one of our visitors recently recommended the following website[...]……

Leave a Reply

Connect with Facebook

*