Click here to Skip to main content
Click here to Skip to main content

Getting File Version Information

, 28 Dec 2006
Rate this:
Please Sign up or sign in to vote.
Getting file version information using C# 2.0.

Version Test Application

Introduction

When I ship a software product for the .NET environment, I would like to know the versions for all the files. From this information, I'll know what's happening if the application gets a crash. Besides that, the file version information can help me to update files properly if I would like to update the application. In this article, I'll show how to get file version information, using C#.

Getting Started

To get version information from a file, we can get it from its assembly. For example, I would like to get the file version from my console application.

Here is the console application code:

    1 using System;
    2 using System.Reflection;
    3 
    4 namespace ConsoleVersionApp
    5 {
    6     class Program
    7     {
    8         static void Main(string[] args)
    9         {
   10             Assembly asm =  Assembly.GetExecutingAssembly();
   11             Console.WriteLine("File Version:");
   12             Console.WriteLine(asm.FullName);
   13 
   14             Console.WriteLine("References :");
   15             AssemblyName[] asmNames = asm.GetReferencedAssemblies();
   16             foreach (AssemblyName nm in asmNames)
   17             {
   18                 Console.WriteLine(nm.FullName);
   19             }
   20 
   21             Console.ReadLine();
   22         }
   23     }
   24 }

From this code, I run the console application that looks as in the picture below:

File Version Wrapper

Now, I would to wrap my file version code. Firstly, I create the AssembyInformation object to keep the file version information. The AssembyInformation class defines a value object. Look at the class diagram below:

The wrapper object I created to get the file version information is PeVersion. From the PeVersion object, we will get information about file version and the referenced file information. The PeVersion object has two public methods, GetVersion(string fileName) and GetVersion(Assembly asm).

   77    public bool GetVersion(string fileName)
   78    {
   79             Assembly asm = null;
   80             try
   81             {
   82                 asm = Assembly.LoadFrom(fileName);
   83             }
   84             catch (Exception err)
   85             {
   86                 this._errMsg = err.Message;
   87                 return false;
   88             }
   89             if (asm != null)
   90             {
   91                 this._info = new AssemblyInformation();
   92                 this._info.Name = asm.GetName().Name;
   93                 this._info.Version = asm.GetName().Version.ToString();
   94                 this._info.FullName = asm.GetName().ToString();
   95             }
   96             else
   97             {
   98                 this._errMsg = "Invalid assembly";
   99                 return false;
  100             }
  101 
  102             return GetReferenceAssembly(asm);
  103     }
  104     public bool GetVersion(Assembly asm)
  105     {
  106             if (asm != null)
  107             {
  108                 this._info = new AssemblyInformation();
  109                 this._info.Name = asm.GetName().Name;
  110                 this._info.Version = asm.GetName().Version.ToString();
  111                 this._info.FullName = asm.GetName().ToString();
  112             }
  113             else
  114             {
  115                 this._errMsg = "Invalid assembly";
  116                 return false;
  117             }
  118 
  119             return GetReferenceAssembly(asm);
  120       }

GetReferenceAssembly() is a private method that is defined as below:

   46    private bool GetReferenceAssembly(Assembly asm)
   47    {
   48             try
   49             {
   50                 AssemblyName[] list = asm.GetReferencedAssemblies();
   51                 if (list.Length > 0)
   52                 {
   53                     AssemblyInformation info = null;
   54                     _lstReferences = new List<ASSEMBLYINFORMATION>();
   55                     for (int i = 0; i < list.Length; i++)
   56                     {
   57                         info = new AssemblyInformation();
   58                         info.Name = list[i].Name;
   59                         info.Version = list[i].Version.ToString();
   60                         info.FullName = list[i].ToString();
   61 
   62                         this._lstReferences.Add(info);
   63                     }
   64                 }
   65             }
   66             catch (Exception err)
   67             {
   68                 this._errMsg = err.Message;
   69                 return false;
   70             }
   71 
   72             return true;
   73    }

I has created the PeVersion and AssemblyInformation objects in a library project, i.e., Pe.Framework.Version. You can download the code from the link above.

Usage

After creating the wrapper class (PeVersion), I will show you how to use this wrapper object. As you know, the PeVersion object can receive input parameters, i.e., file name with full location and the assembly object. Here is a simple usage of the PeVersion object:

    1 using System;
    2 using System.Collections.Generic;
    3 
    4 using Pe.Framework.Version;
    5 namespace PeVersionTestingApp
    6 {
    7     class Program
    8     {
    9         static void Main(string[] args)
   10         {
   11             string file = @"c:\VersionTestApp.exe";
   12             PeVersion ver = new PeVersion();
   13             if (ver.GetVersion(file))
   14             {
   15                 AssemblyInformation obj = ver.CurrentAssemblyInfo;
   16                 Console.WriteLine("File Info:");
   17                 Console.WriteLine("Name : {0}", obj.Name);
   18                 Console.WriteLine("Version : {0}", obj.Version);
   19                 Console.WriteLine("Full Name : {0}", obj.FullName);
   20                 Console.WriteLine("");
   21 
   22                 List<ASSEMBLYINFORMATION> list = ver.ReferenceAssembly;
   23                 if (list.Count > 0)
   24                 {
   25                     Console.WriteLine("References Info:");
   26                     for (int i = 0; i < list.Count; i++)
   27                     {
   28                         AssemblyInformation info = list[i];
   29                         Console.WriteLine("Name : {0}", info.Name);
   30                         Console.WriteLine("Version : {0}", info.Version);
   31                         Console.WriteLine("Full Name : {0}", info.FullName);
   32                         Console.WriteLine("-------------------------------");
   33                     }
   34                 }
   35             }
   36 
   37             Console.ReadLine();
   38         }
   39     }
   40 }

If you want to get the version with the Assembly object as a passing parameter on the PeVersion object, you can use this code:

   1      System.Reflection.Assembly asm = 
               System.Reflection.Assembly.GetExecutingAssembly();
   2      PeVersion ver = new PeVersion();
   3      if (ver.GetVersion(asm))
   4      {
   5      
   6         // do something
   7      
   8      }

File Version Tester Application

The file version tester application is a small application based on the desktop application to check the version and reference files from a file. Using this application, you just pass a file (DLL or EXE Managed) into the application, then the application will check the file version and the references files information. The GUI of this application can looks as below:

You can download this application with the source code above.

Running On Visual Studio

If you have download this source code and run it via Visual Studio, you will get a warning if you put the executable file (*.exe) as the passing parameter into the PeVersion object for checking version information. The warning message looks as below:

To suppress this message, you just uncheck "Warn if no user code on launch" on the debugging options of the IDE of Visual Studio. Look at this picture below:

This warning only shows if you run the application via Visual Studio.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Agus Kurniawan
Founder PE College
Indonesia Indonesia
He gradueted from Sepuluh Nopember Institute of Technology (ITS) in Department of Electrical Engineering, Indonesia. His programming interest is VC++, C#, VB, VB.NET, .NET, VBScript, Delphi, C++ Builder, Assembly,and ASP/ASP.NET. He's founder for PE College(www.pecollege.net), free video tutorial about programming, infrastructure, and computer science. He's currently based in Depok, Indonesia. His blog is http://geeks.netindonesia.net/blogs/agus and http://blog.aguskurniawan.net

Comments and Discussions

 
Questionthe file version maintenance utiltiy PinmemberRahul yadav22-Feb-14 21:35 
QuestionVB.Net File Version of the running .DLL PinmemberL3CodeProject12-Sep-13 12:07 
QuestionGreat Work. PinmemberMrinal Kumar9-Aug-12 22:42 
QuestionNo unload of assemblies Pinmembermagamnak30-Jun-11 22:46 
GeneralMy vote of 4 Pinmembershelby6719-May-11 15:49 
GeneralMy vote of 1 Pinmemberrev3nant13-Feb-11 23:58 
GeneralMy vote of 4 PinmemberStevenCTong20-Sep-10 4:03 
GeneralMy vote of 1 PinmemberMoim Hossain5-Aug-10 22:52 
GeneralMy vote of 1 Pinmembercocowalla26-Oct-09 3:17 
GeneralIncorrect implementation Pinmemberrajeshjj12-Aug-08 6:11 
GeneralRe: Incorrect implementation Pinmemberchaiguy13379-Dec-08 12:15 
GeneralRe: Incorrect implementation Pinmemberalhambra-eidos15-Oct-10 5:46 
GeneralFile version is different from assembly version Pinmemberdmihailescu12-Dec-07 12:10 
Generalwatch outwith this in web applications PinmemberWiebe Tijsma2-Oct-07 5:29 
GeneralRe: watch outwith this in web applications PinmemberSnixtor1-Sep-08 20:43 
QuestionHow to pre-load referenced assemblies [modified] Pinmembersprezzatura28-Aug-07 5:36 
GeneralFinally found what i needed Pinmembermaxlynch29-Mar-07 12:57 
QuestionWhat exactly this code load to memory... Pinmembershreeve@gmail5-Mar-07 0:50 
QuestionWhy not use FileVersionInfo ? PinmemberHCM-VIX30-Jan-07 9:42 
AnswerRe: Why not use FileVersionInfo ? Pinmemberpankajdc13-Sep-07 14:20 
GeneralAssemblyVersion vs. FileVersion PinmemberHeiner Suter16-Jan-07 22:59 
GeneralCool Pinmemberarnoldwid2-Jan-07 23:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 28 Dec 2006
Article Copyright 2006 by Agus Kurniawan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid