Getting application version

This short article shows how to obtain the version information of the application, usable for instance in an about box.

The easiest way is of course to use the Application.ProductVersion string, but that's just what it is, a string.

Here's sample code to display the version of the application:

// using System.Windows.Forms;
  MessageBox.Show("Version is " + Application.ProductVersion);

This will show something like "Version is 1.0.1226.38800". The last two parts are usually set by VS.NET/C# compiler when you compile and correspond to the date and time of day respectively, where 1226 is the number of days since january 1st 2000, and 38800 is the number of seconds since midnight divided by two.

This is all well and good, but how to obtain the version information in their separate numbers so that you can limit it to 1.0 for instance ?

In that case you need to use a different class, FileVersionInfo, as shown here:

// using System.Diagnostics;
  FileVersionInfo verInfo =
  MessageBox.Show("Version is " + verInfo.FileMajorPart.ToString() +
    "." + verInfo.FileMinorPart.ToString());

This will show only "Version is 1.0".

The four parts of the version number 1.0.1226.38800 can be found as the following four properties of the FileVersionInfo class:

  - FileMajorPart
  - FileMinorPart
  - FileBuildPart
  - FilePrivatePart

The class also has other properties with more information.


Share this article!

Follow us!

Find more helpful articles: