Insert SVN version and Build number in your C# AssemblyInfo file

Software version number is quite important. It helps you track what versions have your users when they report something. And when it’s linked to an SVN version number, it’s even better.

Well, with MSBuild Community Task, you can easily automatically generate smart version numbers, you have to:

  • Download MSBuildCommunityTasks
  • Make sure your “svn.exe” binary is in C:\program files\subversion\bin
  • Add this at the end of your .csproject file :

2011-07-02 update: As given in Markus comment, this code is a much better option:

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Import of the MSBuildCommunityTask targets -->
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
 
  <!-- to AssemblyInfo to include svn revision number -->
<Target Name="BeforeBuild">
	<SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(ProgramFiles)\VisualSVN\bin">
	   <Output TaskParameter="Revision" PropertyName="Revision" />
        </SvnVersion>
 
	<FileUpdate Files="Properties\AssemblyInfo.cs"
                Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
                ReplacementText="$1.$2.$3.$(Revision)" />
</Target

You should only have a “</Project>” field left…

Then, you just have to open your project and build your project, it will fail once (missing version.txt file) and then work forever. This will generate your Assembly & AssemblyFile versions like this: Major.Minor.SvnVersion.BuildVersion

In your C# code, to get your version, you just have to add something like that:

1
2
3
4
5
public static String Version {
  get {
    return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  }
}
GD Star Rating
loading...
Insert SVN version and Build number in your C# AssemblyInfo file, 8.0 out of 10 based on 5 ratings
Posted in English. Tags: , . 18 Comments »

18 Responses to “Insert SVN version and Build number in your C# AssemblyInfo file”

  1. Damien Says:

    Hi, thanks for this article,

    but does there is a way to compile the application even if SVN is not installed ?

    Regards

    GD Star Rating
    loading...
  2. Florent Clairambault Says:

    The easiest way I could find is to add a condition on the SvnVersion tag :

    1
    
        <SvnVersion LocalPath="$(MSBuildProjectDirectory)" Condition="Exists('$(ProgramFiles)\subversion\bin\svn.exe')">

    Your build number will be incremented and your SVN revision number won’t be fetched.

    GD Star Rating
    loading...
  3. yorch Says:

    Thanks, just what I was looking for!
    I made a little change on configuration because my SVN binaries are located elsewhere.

    1
    
    <SvnVersion LocalPath="$(MSBuildProjectDirectory)" ToolPath="$(ProgramFiles)\CollabNet Subversion Client">
    GD Star Rating
    loading...
  4. wcoenen Says:

    Interesting. But what do you do if the svn revision number goes over 65535 (i.e. the maximum for any of the 4 version number components) ?

    GD Star Rating
    loading...
  5. Florent Clairambault Says:

    Well, I really don’t know. I have never worked in a so big SVN Repository.

    I think it might just generate a number with the $svnRev%65536.

    GD Star Rating
    loading...
  6. Automate adding/using of the SVN revision to/as the version of a .net project, through a build target « theKindOfMe Says:

    [...] Insert SVN version and Build number in your C# AssemblyInfo file [...]

  7. Florent Clairambault Says:

    Most of the people of my blog are coming for this post. I should really find a better solution. Something between the solution of “theKindOfMe” and mine.

    It would be great to extract build number and svn revision number and just update the AssemblyVersion file without having to specify each property in the “csproj” file.

    I should work on a clean solution to that problem. But that really isn’t fun to do because MSBuild XML language is not really comfortable.

    GD Star Rating
    loading...
  8. chai Says:

    where can I find MSBuild.Community.Tasks.Targets?

    GD Star Rating
    loading...
  9. Florent Clairambault Says:

    You have to download MsBuild Community Tasks here : http://msbuildtasks.tigris.org/

    GD Star Rating
    loading...
  10. Simion Says:

    Hello, can you tell me which changes can be done to get the same for “*.vcproj” project file.
    Tx in advance !

    GD Star Rating
    loading...
  11. Florent Clairambault Says:

    The .vcproj files don’t use the MSBuild format until Visual Studio 2010. And then my guess is that you can pretty much use the MSBuild vcproj file the same way.
    I haven’t tested it because the only C++ I do is on Linux with some Makefile files.

    GD Star Rating
    loading...
  12. Simion Says:

    But is a possibility to use versioning on the .vcproj projects? For instance i have a .dll created in .vcproj project and i want to have versioning on it.

    GD Star Rating
    loading...
  13. Florent Clairambault Says:

    I really have no idea.

    GD Star Rating
    loading...
  14. Insert SVN version and Build number in your C# AssemblyInfo file « 資訊與工作 Says:

    [...] Insert SVN version and Build number in your C# AssemblyInfo file – Florent Clairambault Insert SVN version and Build number in your C# AssemblyInfo file [...]

  15. How to automatically insert SVN Version number on a webpage(assembly)? | PGS Software Says:

    [...] on the solution found on Florent Clairambault Blog here, I thought why not integrate it into the my Web Deployment Project? Let’s give it a chance [...]

  16. Massimiliano Says:

    Hi, i’m trying to using this system.
    i have a little problem. I try to change (from visual studio propertyes interface) minor and minor version.
    i put the version to 1.1.svn.build.
    When i run my application the version come back to 1.0.svn.build. How i can change the major and minor version?
    Thank’s a lot and sorry for my english

    GD Star Rating
    loading...
  17. JR Says:

    Note: This works but when you run the command, the version.txt file won’t contain your svn version. It just increments.

    To check everything is working, go look at your AssemblyInfo.cs file. It should have the right svn number (it just won’t match what the version.txt file says).

    This through me for a loop at first. Thought it might others as well.

    Thanks for sharing however. Great solution. Cheers

    GD Star Rating
    loading...
  18. Markus Says:

    Hi,

    I found this page, which has a solution to the problem of rewriting AssambyInfo every time and instead update it:

    http://thekindofme.wordpress.com/2009/04/27/svn-revision-number-as-version-nunmber-in-vs-through-a-build-target/

    There this code is used:

    <FileUpdate Files=”Properties\AssemblyInfo.cs”
    Regex=”(\d+)\.(\d+)\.(\d+)\.(\d+)”
    ReplacementText=”$1.$2.$3.$(Revision)” />

    GD Star Rating
    loading...

Leave a Reply