This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

.NET Framework
.NET 1.1+

Obtaining File Version Numbers

.NET assemblies can be given three distinct version numbers, each serving a different purpose. It can be useful to obtain these version numbers at run-time. This is made possible with reflection and the use of the FileVersionInfo class.

Version Numbers

As described in the "Windows Forms Application Version Numbers" article, all .NET assemblies have three version numbers that can be set individually and that are added to the compiled executable or DLL files during the build process. The three version numbers have slightly different purposes:

  • Assembly Version. This version number is made up of four parts, called the major, minor, build and revision numbers. Each part is a sixteen bit integer. When displayed as a full version number, the parts are usually separated with full-stop (period) characters. For example, "". The assembly version number is used at run time when locating dependencies. It ensures that the correct versions of all referenced assemblies are loaded. For signed assemblies, having an incorrect assembly version can cause an application to fail to load. For this reason, it is advisable to not change this version number, except when introducing major updates or breaking changes.
  • File Version. This version number can be changed without affecting the loading of referenced assemblies. It is visible in a file's properties when viewed using the Windows Explorer program and is commonly structured as four integer parts, as with the assembly version. It is designed to provide a version number for an individual file, so it is common to change the number for every release of an application. Often the file version is updated automatically during the build process, perhaps incorporating information such as the build date. For example, the version number, "1.2.2012.1108" may be used for an assembly compiled on 11 August 2012.
  • Informational Version. This is displayed in Windows Explorer as the Product Version. The informational version can be formatted as four sixteen bit integers but often includes other text. Usually the informational version is the one that you would use when talking about your products with your users. A common approach is to assign the same information version for all of an application's assemblies.

The three version numbers are defined using assembly attributes, usually with the AssemblyInfo.cs file. The following shows an example of the code used to set them.

[assembly: AssemblyVersion("")]
[assembly: AssemblyFileVersion("1.2.2012.1108")]
[assembly: AssemblyInformationalVersion("1.2 Beta")]

Obtaining Version Information for an Assembly

All three of an assembly's version numbers can be obtained at run time. This is achieved using a mixture of reflection and the FileVersionInfo class, which is found in the System.Diagnostics namespace. If you want to try out the example code in this article, ensure you add the following using directives:

using System.Diagnostics;
using System.Reflection;

Getting the Assembly Version

We'll start by getting the assembly version of an assembly using reflection techniques. First, you need to obtain an Assembly object that represents the item to examine. You can call this object's GetName method to retrieve an AssemblyName object, which contains the assembly version in the Version property.

The following code uses this approach to output the assembly version as it would appear when defined using the sample attributes seen earlier.

Assembly asm = Assembly.GetExecutingAssembly();
Console.WriteLine(asm.GetName().Version);   //

You can retrieve the individual integer parts of the version number using the Major, Minor, Build and Revision properties of the Version object.

Assembly asm = Assembly.GetExecutingAssembly();
Version version = asm.GetName().Version;
Console.WriteLine(version.Major);       // 1
Console.WriteLine(version.Minor);       // 2
Console.WriteLine(version.Build);       // 3
Console.WriteLine(version.Revision);    // 4

Getting the File and Informational Versions

We'll extract the file and informational version numbers using the FileVersionInfo class. Amongst other things, this class obtains version information for files, based upon the file name. To get an instance of the FileVersionInfo class, you can call its static GetVersionInfo method, passing the file name and path to its string parameter. If you want to retrieve details for an assembly that you have obtained using reflection, you need to get its file name from its Location property.

The full file and informational version numbers are held as plain strings within the FileVersionInfo instance. They can be found in the FileVersion and ProductVersion properties, as demonstrated in the following sample code:

Assembly asm = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
Console.WriteLine(fvi.FileVersion);     // 1.2.2012.1108
Console.WriteLine(fvi.ProductVersion);  // 1.2 Beta

To work with the individual integer parts of the two version numbers there are eight available properties. These have names that start with either File or Product, depending upon the version number you need. The names end with MajorPart, MinorPart, BuildPart or PrivatePart, where PrivatePart gives the revision number. If any of the version number parts have been omitted, the relevant properties return zero. You can see this in the ProductBuildPart and ProductPrivatePart values below:

Assembly asm = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
Console.WriteLine(fvi.FileMajorPart);       // 1
Console.WriteLine(fvi.FileMinorPart);       // 2
Console.WriteLine(fvi.FileBuildPart);       // 2012
Console.WriteLine(fvi.FilePrivatePart);     // 1108
Console.WriteLine(fvi.ProductMajorPart);    // 1
Console.WriteLine(fvi.ProductMinorPart);    // 2
Console.WriteLine(fvi.ProductBuildPart);    // 0
Console.WriteLine(fvi.ProductPrivatePart);  // 0

Obtaining Version Information for a File

The FileVersionInfo class is not limited to working with .NET assemblies. You can use it to obtain version information for any file. This is useful when you have external dependencies that are not .NET-based and you need to ensure that the version is correct. The final sample obtains version information for the Notepad utility that is installed with Microsoft Windows.

FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(@"c:\windows\notepad.exe");
Console.WriteLine(fvi.FileVersion);     // 6.1.7600.16385 (win7_rtm.090713-1255)
Console.WriteLine(fvi.ProductVersion);  // 6.1.7600.16385

NB: Most data files do not include version information. If you load the version information for such files, the integer values will all be zero.

11 August 2012