Chris Roberts

Automatically Adding SVN Revision Numbers to Assemblies

• Posted in Programming

Source code version control is one thing that no developer should ever live without. If you're working on some code that isn't in a version control system, stop what you're doing now and get yourself one!

There's no excuse - there are plenty of free ones available that are simple to install and use. My personal favourite is Subversion (SVN). The particular flavour I use is VisualSVN which has a simple server configuration system and a convenient Visual Studio plugin. If you're really new to version control, you could do a lot worse than to read the SVN book, too.

Exact revision numbers are a vital part of our quality control process. All bug reports are logged against a specific revision of our code. If a customer calls to report a bug we insist on having the exact revision number - 'Version 2.0.285'. Bug reported for 'Version 2' just aren't good enough and won't get logged in our bug list.

When bugs are fixed, the revision number which includes the fix is also logged. This gives us exact points in time where the bug existed, and where it was ultimately fixed. It also allows us to pinpoint what was done to fix the bug, and who did it.

Incrementing the revision number which gets displayed in your UI every time it changes is not something that should be done manually, though - it's far too easy to forget and is also completely unnecessary when you can quite easily automate it.

In our build process, we use the MSBuild Community Tasks project to automatically retrieve the revision number of the code from Subversion and add it to our assembly information. At runtime, we use reflection to read this information back out of the assembly for display to the user.

Configuring Your Build Process

First off, download and install the MSI package from the MSBuild Community Tasks website. You'll need to do this on every machine that will build your project.

Next, you'll need to modify the MSBuild script for your project - in other words - your Visual Studio project file (.vbproj or .csproj). You can do this either using your favourite text editor, or from within Visual Studio...

  1. Right-click on your project in the 'Solution Explorer' and select 'Unload Project'.
    If this option doesn't appear, you'll need to go to the Tools -> Options menu and tick the 'Always show solution' option under 'Projects and Solutions', first.
  2. Right-click on the newly 'unloaded' project and you will see an option to edit the project file.
  3. When you've finished your editing, you can right-click again and select 'Reload Project'.

If you look through this file, you will find some <Import Project="..."> statements. Add a new line and add a reference to the MSBuild Community Tasks targets:

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

Directly under this, create a property group to contain the Major, Minor and Build numbers for your project. Note that the 'Revision' will get overwritten by the revision number from SVN:


The standard Visual Studio project template has a <Target Name=”BeforeBuild”> section which is commented out by default. Un-comment this and change it to the following:

<Target Name="BeforeBuild">
  <SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="C:\Program Files\VisualSVN\bin\">
    <Output TaskParameter="Revision" PropertyName="Revision" />
  <Message Text="Version: $(Major).$(Minor).$(Build).$(Revision)" />
  <AssemblyInfo CodeLanguage="VB"
                OutputFile="My Project\AssemblyInfo.vb"
                AssemblyTitle="My Assembly Title"
                AssemblyDescription="My Assembly Description"
                AssemblyCompany="My Company"
                AssemblyProduct="My Product"
                AssemblyCopyright="My Copyright"
                Condition="$(Revision) != 0" />

There are obviously a few things in there that you'll want to change - particularly in the <AssemblyInfo> tag to make it more specific to your product. You may also need to change the 'ToolPath' attribute of the <SvnVersion> tag to the location of your command line SVN tool.

Once you've made these changes, reload your project and try a build. All being well the resulting DLL will include the correct version number.

Displaying the Revision Number in your UI

The final step is to get your revision number showing in your UI for all to see. Luckily, reflection comes to the rescue and allows you to retrieve your entire version number in this (rather long) single line of code:

VersionNo = System.Reflection.Assembly.GetExecutingAssembly().GetName.Version.ToString

Then, proudly display this value in a label on your Windows form, or squirt it out in your HTML for all to see!