WTV: Automatic date-based version numbering for .Net with WhenTheVersion
by Andrew Freemantle • November 5, 2011 • 11 Comments
There’s a trend towards using the software build date as part of the software version number, you know the sort of thing I mean: ‘2011.11.4.xyz‘, like in the footer over at StackOverflow.com, or MyLetts.com..
Wouldn’t it be great if we could get Visual Studio to do this for us automatically on build? I thought so too, so I’ve written a little app to do it
Getting set up with “When The Version”
- Go get a copy of “When The Version” (WTV.exe) from the GitHub download page (or you can build it from the source if you prefer)
- Drop it somewhere on your PC
- Now, in your Visual Studio project, expand the Properties and duplicate the AssemblyInfo.cs / .vb file – I rename the duplicate to be AssemblyInfo.Template.cs:
- Edit the AssemblyInfo.Template.cs file, putting in the placeholders for the date parts like so:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
// Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("{YYYY}.{MM}.{DD}.0")] [assembly: AssemblyFileVersion("{YYYY}.{MM}.{DD}.0")] |
The following values are currently supported:
- {YYYY} – the year
- {MM} – the month
- {DD} – the day
- {SVN} – SubVersion revision number (more on this a bit later)
- Now, open up the Project Properties > Build Events, and add the following Pre-build event command line (on a single line):
|
1 2 3 |
"C:\Path\To\wtv.exe" "$(ProjectDir)Properties\AssemblyInfo.Template.cs" "$(ProjectDir)Properties\AssemblyInfo.cs" |
- Finally, set the Build Action for the new AssemblyInfo.Template.cs file to ‘None’ and Build!
If you get stuck, pop open a Command Prompt and run WTV.exe without any parameters for usage instructions, or paste your full command line to see any helpful error messages:
SubVersion support
If you’re using SubVersion for your source control, WTV can also put the projects SVN revision number into the application version.
There’s only one caveat – .Net Application version number values are limited to a maximum of 32,767 (i.e. they’re Int16′s / short’s). Therefore, if your SVN revision is higher WTV will only use the last 4 digits: e.g.: 32768 -> 2768.
To use the SVN revision number, simply add the {SVN} placeholder to your AssemblyInfo.Template.cs file like so:
|
1 2 |
[assembly: AssemblyVersion("{YYYY}.{MM}.{DD}.{SVN}")] [assembly: AssemblyFileVersion("{YYYY}.{MM}.{DD}.{SVN}")] |
.. and then add a couple of extra parameters to the Pre-build event command line so WTV knows where SubWCRev.exe is, something like this (again, on a single line):
|
1 2 3 4 5 |
"C:\Path\To\WTV.exe" "$(ProjectDir)Properties\AssemblyInfo.Template.cs" "$(ProjectDir)Properties\AssemblyInfo.cs" "C:\Program Files\TortoiseSVN\bin\SubWCRev.exe" "$(SolutionDir)." |
And because the AssemblyInfo.cs is generated, you can remove it from source control (but don’t remove it from your project!).
Finally
Comments and suggestions are welcome here or on the projects issue tracker over at GitHub. I hope you find it useful




Thank you very much for saving me from writing this myself. I had been using template files and wcrev in C++ projects – this tool is so handy now for my c# projects. Thanks!
Hi Tim,
You’re very welcome – thank you for leaving a comment, it makes it even more worthwhile!
Hey, that’s cool!
Now even cooler would be, if it was possible somehow to make it work with environment variables.
I mean such that, if multiple developers use this, but not each has SVN installed in the same place, the SVN path can be taken out of an environment variable containing the svn path.
Thanks Steve, and sure, that would be even cooler!
I’ve updated WTV so the 3rd parameter can be either the SubWCrev.exe path itself, or an Environment Variable that points to the SubWCrev.exe
Head over to https://github.com/AndrewFreemantle/When-The-Version/downloads
Thanks again for the suggestion
Hi Andrew,
Thank you for making this magnificent tool!
As I’ve used it before, I want to use it with my new projects from now on, there is one thing I don’t get to work though…
The SVN SubWCRev.exe… Visual Studio keeps complaining that it can’t find the exe…
I’ve included in the standard path and I’m running out of ideas here.. I’ve even compiled the sources with some extra information where the current files are..
Thanks,
Mathew.
As for some more information..
The Pre-build event command line looks like this:
“c:\wtv\wtv.exe” “$(ProjectDir)Properties\AssemblyInfo.Template.cs” “$(ProjectDir)Properties\AssemblyInfo.cs” “C:\Program Files\TortoiseSVN\bin\SubWCRev.exe\SubWCRev.exe” “$(SolutionDir)”
After I’ve started a build I get this message:
Error The command “”c:\wtv\wtv.exe” “C:\Projects\FAME\Configuration\Configurator\Properties\AssemblyInfo.Template.cs” “C:\Projects\FAME\Configuration\Configurator\Properties\AssemblyInfo.cs” “C:\Program Files\TortoiseSVN\bin\SubWCRev.exe” “C:\Projects\FAME\”" exited with code -1. Configurator
Thank you very much Andrew for the tool and your explanations.
Best regards,
David.
Hi Mathew, thank you for taking the time to post a comment
It looks like your path to SubWCRev.exe is at fault here – the Pre-build event has the argument “…bin\SubWCRev.exe\SubWCRev.exe” < - that's an extra level it doesn't need.
However, that doesn’t seem to matter, as it gets sorted according to your build error message – i.e. the path to SubWCRev.exe looks fine.
What I’d suggest is, take that build error message, and paste the command into a Command Prompt window as WTV will give you a more detailed error message.
Do let me know how you get on!
The parts for version info actually are unsigned shorts. Therefore the maximum values are 65535 ie ushort.MaxValue.
Nonetheless you did a cool job of providing such an easy way of automatic numbering.
Thank you!
regards
Sigi
Thanks for kind words and the ushort pointer, Sigi
I just used in my project at work. Thanks a lot