• WTV: Automatic date-based version numbering for .Net with WhenTheVersion

    by  • 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”

    1. 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)
    2. Drop it somewhere on your PC
    3. 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:

    1. Edit the AssemblyInfo.Template.cs file, putting in the placeholders for the date parts like so:

    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)

     

    1. Now, open up the Project Properties > Build Events, and add the following Pre-build event command line (on a single line):

     

    1. 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:

     

    .. 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):

     
    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 :)

    About Andrew Freemantle

    11 Responses to WTV: Automatic date-based version numbering for .Net with WhenTheVersion

    1. tim
      February 10, 2012 at 7:30 pm

      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!

    2. February 12, 2012 at 2:00 pm

      Hi Tim,
      You’re very welcome – thank you for leaving a comment, it makes it even more worthwhile! :)

    3. steve
      April 13, 2012 at 11:08 am

      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.

    4. April 13, 2012 at 4:24 pm

      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

    5. Mathew
      August 8, 2012 at 3:04 pm

      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.

    6. Mathew
      August 10, 2012 at 9:52 am

      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

    7. David Roberts
      August 29, 2012 at 9:57 pm

      Thank you very much Andrew for the tool and your explanations.
      Best regards,
      David.

    8. September 6, 2012 at 4:19 pm

      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!

    9. Sigi
      September 26, 2012 at 7:15 am

      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

    10. September 27, 2012 at 1:13 pm

      Thanks for kind words and the ushort pointer, Sigi :)

    11. lszk
      January 8, 2013 at 2:46 pm

      I just used in my project at work. Thanks a lot

    Leave a Reply

    Your email address will not be published.

    Your name *

    Your website

    *