Feb 022013
Share...Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on LinkedInPin on PinterestShare on TumblrShare on RedditDigg this

If you, like me, are using Subversion as your primary Version System and want to embed the SVN revision number directly in the assembly, but don’t know how, this post will try to explain it.


What do you need?

Before proceeding any further, you would need to download the following two packages (free!)

I assume that you are familiar with MsBuild and how to work with SVN.

What is the process to follow

In the explanation I assume the build process is made out of a Build Server (Hudson, Bamboo, Team City, or others..) but exactly the same process applies if the compilation is done manually.

In the image above, I am showing two different steps:

  1. Developer, committing the code in the SVN repository
  2. In my case, a Continuous Integration server that compiles the project. Steps that the Build Service has to perform are:
    • Download the latest version of the source code
    • Change the AssemblyInfo.cs file with the Repository Revision number. I will show how to do this, just keep reading.
    • Compile the solution.

It’s pretty much clear and simple to follow this steps.

Preprocessing the AssemblyInfo.cs in MsBuild

The main point of the whole process in the building (compilation) of the project.
The following MsBuild Target will help you with the “pre-processing”, which means, changing the AssemblyInfo.cs file before the code is compiled. In this way we are able to embed the revision number.

Two tasks are used from the MsBuild Community Tasks

  • SvnVersion
  • FileUpdate

so on top of your MsBuild file you would need to include the following declaration:


and use this target to be executed BEFORE the main compilation.
For brevity I am including only the Target itself, as your build process could vary.



Pretty much simple. Isn’t it?
In my case the revision number is 5683 and you may see the end result in the picture below:


I believe that this is a very effective and simple way of including the revision number directly in the assembly, which makes the revision tracking simpler overall.
If you think there are other and more effective ways, please let me know!

    Share...Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on LinkedInPin on PinterestShare on TumblrShare on RedditDigg this

    I'm a Software Developer and Solution Architect interested in Software Development, Object-Oriented Design and Software Architecture all this especially bound to the Microsoft.NET platform.Feel free to contact me or know more in the about section

      9 Responses to “Embedding SVN revision number at compile time with MsBuild”

    1. We had to use RevNr / 10000 . REvNr % 10000 on the last 2 parts of the Versioning because of Revisions up and above of 10k.


    2. Ist also possible to get the LastChangedRevision instead of the revision.?

    3. Hi Zoran,

      I’ve tried to do this but i don’t have svnVersion.exe. I’m using tortoise SVN 1.7.

      Thank you!

    4. Hi Zoran,

      I get the message:
      Revision: 27210
      Updating File “c:\work\Projects\trunk\Gr\Lisp\Prac\MyProject\Properties\AssemblyIn
      Replaced matches with “$1.$2.$3.27210”.

      but I see no changes in the AsseemblyInfo.cs:
      [assembly: AssemblyVersion(“”)]
      [assembly: AssemblyFileVersion(“”)]

      What is wrong?


    5. Nice post!

      We use MSBuild Versioning (versioning.codeplex.com) to perform the pre-build processing.

      At the end the build date information is in the 3d group and the svn-revision number in the 4th digit group, as in your case. Since our revision numbers are still low enough (3 digits), we use the 4th digit to signal commit status. If the assembly was built using non committed code (dirty) it ends with 1, otherwise wit 0.


      • Hi Dalibor,
        I checked the versioning.codeplex.com that you mentioned, and indeed is a good tool and probaly we will try to integrate it. I think that the solution here expressed mimics pretty much what that tool does.
        I have to add that what I’ve described in the above post, works well when it comes to run the process in an CI environment. I noticed one small annoyance when building the solution on the workstation. Before the compilation starts, the assemblyinfo.cs is changed, and then code get’s compiled. What is missing in this process is to re-change again the AssemblyInfo.cs files to what they’ve been before the compilation process, otherwise you have to delete those files manually and reload them from SVN. Nothing serious but worth investigating.


    Leave a Reply

    hevessy.eko keely