<p>Is there any way to include the SVN repository revision number in the version string of a .NET assembly?  Something like Major.Minor.SVNRev</p>

<p>I've seen mention of doing this with something like CC.NET (although on ASP.NET actually), but is there any way to do it without any extra software?  I've done similar things in C/C++ before using build batch scripts, but in was accomplished by reading the version number, then having the script write out a file called "ver.h" everytime with something to the effect of:</p>

<pre><code>#define MAJORVER 4
#define MINORVER 23
#define SOURCEVER 965
</code></pre>

<p>We would then use these defines to generate the version string.</p>

<p>Is something like this possible for .NET?</p>

[{'answer_id': 12189, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 4, 'selected': True, 'text': '<p>Have a look at SubWCRev - <a href="http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html" rel="noreferrer">http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-subwcrev.html</a></p>\n\n<p>The assembly version numbers are usually in assemblyinfo.cs</p>\n'}, {'answer_id': 12261, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 2, 'selected': False, 'text': '<p>svn info, tells you the version you are on, you can make a "pre-build" event in VS on your project to generate the assemblyinfo.cs by running svn info and parsing its results with a home grown command line app.</p>\n\n<p>I have done this before, but quickly switched to just having ccnet pass it as a variable to nant.</p>\n'}, {'answer_id': 58379, 'author': 'Eric Schoonover', 'author_id': 3957, 'author_profile': 'https://Stackoverflow.com/users/3957', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to update the version number in a projects AssemblyInfo.cs you may be interested in this article:</p>\n\n<p><a href="http://www.codeproject.com/KB/architecture/svn_visual_studio.aspx" rel="nofollow noreferrer">CodeProject: Use Subversion Revision numbers in your Visual Studio Projects</a></p>\n\n<blockquote>\n  <p>If you enable SVN Keywords then every time you check in the project Subversion scans your files for certain "keywords" and replaces the keywords with some information.</p>\n  \n  <p>For example, At the top of my source files I would create a header contain the following keywords:</p>\n  \n  <p>\'$Author:$<br>\n  \'$Id:$<br>\n  \'$Rev:$</p>\n  \n  <p>When I check this file into Subversion these keywords are replaced with the following:</p>\n  \n  <p>\'$Author: paulbetteridge $<br>\n  \'$Id: myfile.vb 145 2008-07-16 15:24:29Z paulbetteridge $<br>\n  \'$Rev: 145 $  </p>\n</blockquote>\n'}, {'answer_id': 653382, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Read/skim these docs:</p>\n\n<p><a href="http://www.codeproject.com/KB/library/subversion_using_dotsvn.aspx?display=PrintAll" rel="noreferrer">Accessing the Subversion repository from .NET using DotSVN</a></p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/t9883dzc.aspx" rel="noreferrer">How to: Write a Task</a></p>\n\n<p><a href="http://florent.clairambault.fr/insert-svn-version-and-build-number-in-your-c-assemblyinfo-file" rel="noreferrer">Insert SVN version and Build number in your C# AssemblyInfo file</a></p>\n\n<p><a href="http://msdn.microsoft.com/en-us/magazine/cc163589.aspx" rel="noreferrer">Compiling Apps With Custom Tasks For The Microsoft Build Engine</a></p>\n\n<p>The MSBuildCommunityTasks svnversion mentioned in third reference would not perform with svn on Mac 10.5.6 and VS2008 C# project build inside Parallels hosting Vista (ie., across OS).</p>\n\n<p>Write your own task to retrieve revision from repository using DotSVN:</p>\n\n<pre><code>using System;\nusing Microsoft.Build.Framework;\nusing Microsoft.Build.Utilities;\nusing DotSVN.Common;\nusing DotSVN.Common.Entities;\nusing DotSVN.Common.Util;\nusing DotSVN.Server.RepositoryAccess;\n\nnamespace GetSVNVersion\n{\n    public class GetRevision : Task\n    {\n        [Required]\n        public string Repository { get; set; }\n        [Output]\n        public string Revision { get; set; }\n\n        public override bool Execute()\n        {\n            ISVNRepository repo;\n            bool connected = true;\n            try\n            {\n                repo = SVNRepositoryFactory.Create(new SVNURL(Repository));\n                repo.OpenRepository();\n                Revision = repo.GetLatestRevision().ToString();\n                Log.LogCommandLine(Repository + " is revision " + Revision);\n                repo.CloseRepository();\n            }\n            catch(Exception e)\n            {\n                Log.LogError("Error retrieving revision number for " + Repository + ": " + e.Message);\n                connected = false;\n            }\n            return connected;\n        }\n    }\n}\n</code></pre>\n\n<p>This way allows the repository path to be "file:///Y:/repo" where Y: is a Mac directory mapped into Vista.</p>\n'}, {'answer_id': 654344, 'author': 'Wim Coenen', 'author_id': 52626, 'author_profile': 'https://Stackoverflow.com/users/52626', 'pm_score': 3, 'selected': False, 'text': "<p>It is possible but you shouldn't: the components of the assembly version string are limited to 16-bit numbers (max 65535). Subversion revision numbers can easily become bigger than that so at some point the compiler is suddenly going to complain.</p>\n"}, {'answer_id': 965790, 'author': 'ferventcoder', 'author_id': 18475, 'author_profile': 'https://Stackoverflow.com/users/18475', 'pm_score': 0, 'selected': False, 'text': '<p>You can use a shared Assembly Version file that you can reference in all of your projects.</p>\n\n<p><a href="http://uppercut.googlecode.com" rel="nofollow noreferrer">UppercuT</a> does this - <a href="http://ferventcoder.com/archive/2009/05/21/uppercut---automated-builds---versionbuilder.aspx" rel="nofollow noreferrer">http://ferventcoder.com/archive/2009/05/21/uppercut---automated-builds---versionbuilder.aspx</a></p>\n\n<p>This will give you an idea of what you can do to get versions in your assemblies.</p>\n'}, {'answer_id': 9237374, 'author': 'Tim', 'author_id': 10755, 'author_profile': 'https://Stackoverflow.com/users/10755', 'pm_score': 2, 'selected': False, 'text': '<p>Another answer mentioned that SVN revision number might not be a good idea because of the limit on the size of the number.  </p>\n\n<p>The following link provides not only an SNV revision number, but also a date version info template.  </p>\n\n<p>Adding this to a .NET project is simple - very little work needs to be done.</p>\n\n<p>Here is a github project that addresses this\n<a href="https://github.com/AndrewFreemantle/When-The-Version/downloads" rel="nofollow">https://github.com/AndrewFreemantle/When-The-Version/downloads</a></p>\n\n<p>The following url may load slowly but is a step-by-step explanation of how to make this work  (easy and short 3 or 4 steps)</p>\n\n<p><a href="http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/" rel="nofollow">http://www.fatlemon.co.uk/2011/11/wtv-automatic-date-based-version-numbering-for-net-with-whentheversion/</a></p>\n'}, {'answer_id': 14195519, 'author': 'R. Schreurs', 'author_id': 456456, 'author_profile': 'https://Stackoverflow.com/users/456456', 'pm_score': 5, 'selected': False, 'text': '<p>Here\'s and C# example for updating the revision info in the assembly automatically. It is based on the answer by Will Dean, which is not very elaborate.</p>\n\n<p>Example :</p>\n\n<ol>\n<li>Copy AssemblyInfo.cs to AssemblyInfoTemplate.cs in the project\'s\nfolder <em>Properties</em>.</li>\n<li>Change the <em>Build Action</em> to <em>None</em> for AssemblyInfoTemplate.cs.</li>\n<li><p>Modify the line with the AssemblyFileVersion to: </p>\n\n<p><code>[assembly: AssemblyFileVersion("1.0.0.$WCREV$")]</code></p></li>\n<li><p>Consider adding: </p>\n\n<p><code>[assembly: AssemblyInformationalVersion("Build date: $WCNOW=%Y-%m-%d %H:%M:%S$; Revision date: $WCDATE=%Y-%m-%d %H:%M:%S$; Revision(s) in working copy: $WCRANGE$$WCMODS?; WARNING working copy had uncommitted modifications:$.")]</code>, </p>\n\n<p>which will give details about the revision status of the source the assembly was build from.</p></li>\n<li><p>Add the following Pre-build event to the project file properties:</p>\n\n<p><code>subwcrev "$(SolutionDir)." "$(ProjectDir)Properties\\AssemblyInfoTemplate.cs" "$(ProjectDir)Properties\\AssemblyInfo.cs" -f</code></p></li>\n<li><p>Consider adding AssemblyInfo.cs to the svn ignore list. Substituted revision numbers and dates will modify the file, which results in insignificant changes and revisions and $WCMODS$ will evaluate to true. AssemblyInfo.cs must, of course, be included in the project.</p></li>\n</ol>\n\n<p>In response to the objections by Wim Coenen, I noticed that, in contrast to what was suggested by Darryl, the AssemblyFileVersion also does <strong>not</strong> support numbers above 2^16. The build will complete, but the property <em>File Version</em> in the actual assembly will be AssemblyFileVersion modulo 65536. Thus, 1.0.0.65536 as well as 1.0.0.131072 will yield 1.0.0.0, etc. In this example, there is always the true revision number in the AssemblyInformationalVersion property. You could leave out step 3, if you consider this a significant issue.</p>\n\n<p>Edit: some additional info after having used this solution for a while.</p>\n\n<ol>\n<li>It now use AssemblyInfo.cst rather than AssemblyInfoTemplate.cs, because it will automatically have <em>Build Action</em> option <em>None</em>, and it will not clutter you Error list, but you\'ll loose syntax highlighting.</li>\n<li><p>I\'ve added two tests to my AssemblyInfo.cst files:</p>\n\n<pre><code>#if(!DEBUG)    \n    $WCMODS?#error Working copy has uncommitted modifications, please commit all modifications before creating a release build.:$ \n#endif \n#if(!DEBUG)       \n    $WCMIXED?#error Working copy has multiple revisions, please update to the latest revision before creating a release build.:$ \n#endif\n</code></pre>\n\n<p>Using this, you will normally have to perform a complete SVN Update, after a commit and before you can do a successful release build. Otherwise, $WCMIXED will be true. This seems to be caused by the fact that the committed files re at head revision after the commit, but other files not.</p></li>\n<li>I have had some doubts whether the first parameter to subwcrev, "$(SolutionDir)", which sets the scope for checking svn version info, does always work as desired. Maybe, it should be $(ProjectDir), if you are content if each individual assembly is in a consistent revision.</li>\n</ol>\n\n<p><strong>Addition</strong>\n<em>To answer the comment by @tommylux.</em></p>\n\n<p>SubWcRev can be used for any file in you project. If you want to display revision info in a web page, you could use this VersionInfo template:</p>\n\n<pre><code>public class VersionInfo\n{       \n    public const int RevisionNumber = $WCREV$;\n    public const string BuildDate = "$WCNOW=%Y-%m-%d %H:%M:%S$";\n    public const string RevisionDate = "$WCDATE=%Y-%m-%d %H:%M:%S$";\n    public const string RevisionsInWorkingCopy = "$WCRANGE$";\n    public const bool UncommitedModification = $WCMODS?true:false$;\n}\n</code></pre>\n\n<p>Add a pre-build event just like the one for AssemblyInfo.cst and you will have easy access to all relevant SubVersion info.</p>\n'}]
['https://Stackoverflow.com/questions/12176', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/194/']