Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / Visual Basic
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
27 Apr 2013CPOL2 min read 18.1K   6   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:

XML
<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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalhope to see more tricks like this... Pin
Southmountain30-Jan-20 7:00
Southmountain30-Jan-20 7:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.