Get application version, build and build date

By DimitriC at April 13, 2010 15:09
Filed Under: Programming, tips & tricks

In one of my console applications I wanted to show the assembly version, build and build date in the console window.

 

For getting the assembly version and build, the System-namespace has a Version-object which has properties like MajorVersion, MinorVersion and Build that you can use. When you call the ToString()-method on this object, you will get the full version information.

   1: Version version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

Now you can use the properties to build your text message:

   1: string versionMsg = "Version: " + version.ToString() + " - Build: " + version.Build + ";

 

The problem is that the Build-property remains 0. To solve this, you must use wildcards in the AssemblyInfo.cs file. This will allow for a build-number to be generated:

   1: [assembly: AssemblyVersion("1.0.*")]

 

If you re-run the program, a build number will be visible.

 

Now getting the build date from all of this is tricky too. The Version.Build consists the number of days since January 1, 2000. The Version.Revision property counts the seconds since midnight which you have to multiply by 2 to get the original:

   1: DateTime buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + // days since January 1, 2000 
   2:     TimeSpan.TicksPerSecond * 2 * version.Revision)); // seconds since midnight, today

 

Now you have all the information you need to build the final versionMsg:

   1: string versionMsg = "Version: " + version.ToString() + " - Build: " + version.Build + " - Build Date: " + buildDateTime.ToString();
Comments are closed