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

    by  • November 5, 2011 • 2 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:
    
    // 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)

     

    1. Now, open up the Project Properties > Build Events, and add the following Pre-build event command line (on a single line):
    
    "C:\Path\To\wtv.exe"
      "$(ProjectDir)Properties\AssemblyInfo.Template.cs"
      "$(ProjectDir)Properties\AssemblyInfo.cs"
    

     

    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:

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

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

    About Andrew Freemantle

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

    Leave a Reply

    Your email address will not be published.

    Your name *

    Your website

    *