Jun 6, 2008

A Better way to Update your .NET Assembly Versions

I use Visual Studio 2008 and the Eclipse IDEs and I have been manually updating my version numbers for a long time; mostly because I don't like the wildcard defaults in Visual Studio for assembly numbering. I found a solution for both IDEs although it only works out of the box for Visual Studio.
UpdateVersion is a free .NET application that includes source code and the download page contains a nice user's guide on all the command line flags. The program takes as input, a path to an existing file (like AssemblyInfo.cs) that has your build information and replaces the data with a new string based on the format you specify in command line parameters. You can easily hook this into your automated build process using the Build Event tab of the project properties dialog as shown in the image.

In my case I wanted to use the BuildDay option which makes the build number out of the last digit of the year and the day of the year. The flag for that option is: -b BuildDay. I wanted a simple increment for the revision and that flag is: -r Increment.
In the Pre-build event command line I added two lines. The first line runs UpdateVersion and creates a new AssemblyInfo_new.cs file.
C:\\UpdateVersion.exe -b BuildDay -r Increment -i $(ProjectDir)Properties\AssemblyInfo.cs -o AssemblyInfo_new.cs

Then since I want this file to replace my existing file I added a second line
move AssemblyInfo_new.cs $(ProjectDir)Properties\AssemblyInfo.cs

All that is left is to build the project and the build and revision numbers automatically increment in my preferred format. If your preferred format isn't included, you can modify the source code and make it do what you want. The existing options are pretty flexible though and will probably satisfy most people.

When using it for Eclipse I want to have it update a .h file that holds a string with my version information. I only had to change the regular expression routine to make the leading and trailing quote symbols optional (\"?) instead of required. Now in my header file I have

const char REVISION_MSG = "AssemblyVersion(1.0.8158.2)";

Every time I build the string automatically updates. In Eclipse I integrated the tool by using the projects property dialog and selecting the C/C++ Build Section. In the area to the right I selected the Build Step tab and then entered the command in the Pre-build step area as shown in the image.

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.