Tag Archives: MSBuild

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();
  }
}