Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Visual Studio 2010 Reading SVN Revision and Updating File and Product Version (Easy Way)

0.00/5 (No votes)
27 Apr 2013 1  
Easy Assembly Version reflecting SVN working revision

Introduction

Visual Studio provides you two ways of updating your assembly, product and file. You can update them yourself, or let Visual Studio set the version to 1.0.*, meaning that the resulting version has nothing to relate with. In this tip, I will show a simple way to implement SVN revision number into file version.

Background

This article implements the functions of SubWCRev.exe provided with TortoiseSVN. It is mainly aimed at Visual Studio 2010 with VisualSVN for C# projects, but the same process can be applied for other configurations.

Using the Code

"SubWCRev is a Windows console program which can be used to read the status of a Subversion working copy and optionally perform keyword substitution in a template file. This is often used as part of the build process as a means of incorporating working copy information into the object you are building. Typically it might be used to include the revision number in an “About” box." - This is the intended use, so let's make use of it.

SubWCRev is a command line application, so we can make use of it on the project's 'pre-build event command line'. We will go to read the working copy revision, update the file Assembly.cs, and then build our project.

In order for SubWCRev to update our Assembly.cs, we need to provide it with a template file, for this just create a copy of Assembly.cs and name it Assembly.tmpl. Now open Assembly.tmpl and replace the AssemblyVersion and AssemblyFileVersion with the following lines:

[assembly: AssemblyVersion("1.0.0.$WCREV$")]
[assembly: AssemblyFileVersion("1.0.0.$WCREV$")] 

Every occurrence of $WCREV$ will be replaced by the working copy revision number.

Now we can edit the pre-build event:

cd [TortoiseSVNInstallDir]\bin  
SubWCRev "WorkingCopyPath" "Assemby.tmpl Path" "Assembly.cs Path"  

If we just build the project now, the file Assembly.cs gets updated correctly, but the built assembly does not. The solution to this is very easy, open your .csproj file, copy this line:

 <UseHostCompilerIfAvailable>False</UseHostCompilerIfAvailable>

And paste it right before the <PreBuildEvent> tag. Build your solution twice, and check that your FileVersion is 1.0.0.[SVNRevision].

Hope this helps some smaller developers with version control.

Points of Interest

While this will work OK for smaller projects, it still relies on the revision number being less than 65535.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here