Posted by: Cirilo Meggiolaro | 03/15/2009

Tip of the day #152 – Retrieving file version information

Let’s check today how to retrieve file version of a file. To accomplish that we are going to use the FileVersionInfo class available under the System.Diagnostics namespace.

Methods

A static method named GetVersionInfo is available and is the responsible for gathering information about a file and storing it on a whole bunch of properties that are listed on the next topic:

public static FileVersionInfo GetVersionInfo(string fileName);

The information retrieved is generally stored as metadata inside the file so not all file types are going to be retrieved by the function such as text files. Binary files such as exe and dll’s contain those metadata.

Properties

The following read-only properties are available:

  • string Comments: Gets the file comments;
  • string CompanyName: Gets the name of the company that produced the file;
  • int FileBuildPart: Gets the build number of the file;
  • string FileDescription: Gets the description of the file;
  • int FileMajorPart: Gets the major part of the version number
  • int FileMinorPart: Gets the minor part of the version number of the file
  • string FileName: Gets the name of the file;
  • int FilePrivatePart: Gets the file private part number;
  • string FileVersion: Gets the file version number;
  • string InternalName: Gets the internal name of the file, if it exists;
  • bool IsDebug: Gets a value that specifies whether the file contains debugging information or is compiled with debugging features enabled;
  • bool IsPatched: Gets a value that specifies whether the file has been modified and is not identical to the original shipping file of the same version number;
  • bool IsPreRelease: Gets a value that specifies whether the file is a development version, rather than a commercially released product;
  • bool IsPrivateBuild: Gets a value that specifies whether the file was built using standard release procedures;
  • bool IsSpecialBuild: Gets a value that specifies whether the file is a special build;
  • string Language: Gets the default language string for the version info block;
  • string LegalCopyright: Gets all copyright notices that apply to the specified file;
  • string LegalTrademarks: Gets the trademarks and registered trademarks that apply to the file;
  • string OriginalFilename: Gets the name the file was created with;
  • string PrivateBuild: Gets information about a private version of the file;
  • int ProductBuildPart: Gets the build number of the product this file is associated with;
  • int ProductMajorPart: Gets the major part of the version number for the product this file is associated with;
  • int ProductMinorPart: Gets the minor part of the version number for the product the file is associated with;
  • string ProductName: Gets the name of the product this file is distributed with;
  • int ProductPrivatePart: Gets the private part number of the product this file is associated with;
  • string ProductVersion: Gets the version of the product this file is distributed with;
  • string SpecialBuild: Gets the special build information for the file.

How to…

The first task is to create an instance of the FileVersionInfo class using the static GetVersionInfo method:

FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(@”C:\Windows\Notepad.exe”);

Once you have a valid instance of FileVersionInfo type, you may access the read-only properties to retrieve the information you are looking for.

Console.WriteLine(versionInfo.OriginalFilename);
Console.WriteLine(versionInfo.FileVersion);
Console.WriteLine(versionInfo.InternalName);
Console.WriteLine(versionInfo.IsDebug);
Console.WriteLine(versionInfo.Language);
Console.WriteLine(versionInfo.LegalTrademarks);

Output:
NOTEPAD.EXE
5.1.2600.2180
Notepad
False
English (United States)


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: