C# .Net : How works automatic assembly versioning

When you choose to automatically generate a new version of your assembly at compilation, you have to set in the AssemblyInfo.cs file, this line :

1
2
3
4
5
6
7
8
9
10
// 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 Build and Revision Numbers 
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]

But what do these automatically generated numbers mean ? Well, chances are you don’t care. But one day after using this features for years I decided that I would.

  • The build number is the number of days since 01/01/2000
  • The revision number if the number of 2 seconds periods since 00:00 of this day

So… To get the compilation date of an assembly from its version, you would have to do :

1
2
var version = Assembly.GetExecutingAssembly().GetName().Version;
var date = new DateTime( 2000, 01, 01 ).AddDays( version.Build ).AddSeconds( version.Revision * 2 );

This feature is great if your need some atomic versionning of a particular library for instance. Each program using your library has its own version and you don’t risk to break working feature in future release of your library. And if you want to force the use of a newer version of a library within a particular application, you can use some assembly redirection.
But you should also note that if you deploy your assembly in the GAC (like it’s required to do with Sharepoint), you will have tons of “garbage” versions of your assembly. And there’s not “easy fix” for that.

5 Responses to “C# .Net : How works automatic assembly versioning”

  1. Alexandr Says:

    For .NET 2.0 this is solution:
    DateTime date = new DateTime(2000, 01, 01). AddDays(version.Build).AddSeconds(version.Revision * 2);

  2. Florent Clairambault Says:

    Yes, thank you very much, I mixed up everything. I fixed it.

  3. Peter Holpar Says:

    See the link below for a simple WPF-based utility that applies a similar algorithm in practice to make the conversion between version number and build date: http://pholpar.wordpress.com/2011/01/29/how-to-get-the-assembly-version-number-from-the-build-date-and-vice-versa/

    Peter

  4. Joe Lewis Says:

    What TimeZone does the automatic versioning use? Does it use UTC, or local time zone, and does it consider daylight saving time? I would like to use your code to calculate exact time of compilation, and if it does not use UTC then I don’t think it is possible, since the application can run on computers with different time zone.

  5. Florent Clairambault Says:

    I’m sorry but I don’t have the time to test it now. If someone knows the answer, I would be thankful if he could answer it.

Leave a Reply