Click here to Skip to main content
15,881,840 members
Articles / Programming Languages / C#
Article

Getting File Version Information

Rate me:
Please Sign up or sign in to vote.
3.46/5 (14 votes)
28 Dec 20062 min read 202K   4.4K   50   24
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:

C#
    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:

Image 2

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:

Image 3

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).

C#
   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:

C#
   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:

C#
    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:

C#
   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:

Image 4

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:

Image 5

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:

Image 6

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


Written By
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

 
Questiongood idea Pin
Southmountain4-Jun-22 10:22
Southmountain4-Jun-22 10:22 
QuestionCan i modify this for my personal use Pin
abhi075130-Apr-15 6:42
professionalabhi075130-Apr-15 6:42 
Questionthe file version maintenance utiltiy Pin
Rahul yadav22-Feb-14 20:35
Rahul yadav22-Feb-14 20:35 
QuestionVB.Net File Version of the running .DLL Pin
L3CodeProject12-Sep-13 11:07
L3CodeProject12-Sep-13 11:07 
QuestionGreat Work. Pin
Mrinal Kumar9-Aug-12 21:42
Mrinal Kumar9-Aug-12 21:42 
QuestionNo unload of assemblies Pin
magamnak30-Jun-11 21:46
magamnak30-Jun-11 21:46 
GeneralMy vote of 4 Pin
shelby6719-May-11 14:49
shelby6719-May-11 14:49 
GeneralMy vote of 1 Pin
rev3nant13-Feb-11 22:58
rev3nant13-Feb-11 22:58 
GeneralMy vote of 4 Pin
OptimumCode20-Sep-10 3:03
OptimumCode20-Sep-10 3:03 
GeneralMy vote of 1 Pin
Moim Hossain5-Aug-10 21:52
Moim Hossain5-Aug-10 21:52 
GeneralMy vote of 1 Pin
cocowalla26-Oct-09 2:17
cocowalla26-Oct-09 2:17 
GeneralIncorrect implementation Pin
rajeshjj12-Aug-08 5:11
rajeshjj12-Aug-08 5:11 
GeneralRe: Incorrect implementation Pin
chaiguy13379-Dec-08 11:15
chaiguy13379-Dec-08 11:15 
GeneralRe: Incorrect implementation Pin
kiquenet.com15-Oct-10 4:46
professionalkiquenet.com15-Oct-10 4:46 
GeneralFile version is different from assembly version Pin
dmihailescu12-Dec-07 11:10
dmihailescu12-Dec-07 11:10 
Generalwatch outwith this in web applications Pin
User 1942892-Oct-07 4:29
User 1942892-Oct-07 4:29 
GeneralRe: watch outwith this in web applications Pin
Snixtor1-Sep-08 19:43
Snixtor1-Sep-08 19:43 
QuestionHow to pre-load referenced assemblies [modified] Pin
sprezzatura28-Aug-07 4:36
sprezzatura28-Aug-07 4:36 
GeneralFinally found what i needed Pin
maxlynch29-Mar-07 11:57
maxlynch29-Mar-07 11:57 
QuestionWhat exactly this code load to memory... Pin
shreeve@gmail4-Mar-07 23:50
shreeve@gmail4-Mar-07 23:50 
QuestionWhy not use FileVersionInfo ? Pin
HCM-VIX30-Jan-07 8:42
HCM-VIX30-Jan-07 8:42 
I see you are using Assembly.LoadFrom in your application in order to get the file name, version, etc.. and you created a class to store assembly information.
You dont need a new class it already exists in .NET .
Take a look at class System.Diagnostics.FileVersionInfo .


AnswerRe: Why not use FileVersionInfo ? Pin
pankajdc13-Sep-07 13:20
pankajdc13-Sep-07 13:20 
GeneralAssemblyVersion vs. FileVersion Pin
User 373047016-Jan-07 21:59
User 373047016-Jan-07 21:59 
GeneralCool Pin
arnoldwid2-Jan-07 22:45
arnoldwid2-Jan-07 22:45 

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

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