C# Tips and Tricks #20 – Get Assembly File Version

Do you want to get assembly version or assembly file version in your C# code?. This blog post exactly explains how you can do it with simple code.

When you check the Project properties your .NET Application , you would notice the following properties.

  • AssemblyVersion
  • AssemblyFileVersion
How to get the Assembly File Version in C# ?

Its pretty easy to get the Assembly Version of the project using the Version property as shown below.

Assembly.GetEntryAssembly().GetName().Version;

but , how do one get the assembly file version number ?.

How to get the Assembly File Version in C# ?

When you right click on the assembly , you would be able to see the Assembly File version as shown below.

How to get the Assembly File Version in C# ?

To get this property , you can use reflection and retreive it as shown below.

public static string GetAssemblyFileVersion()
{
             System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
             FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location);
             return fileVersion.FileVersion;
 }

Full code snippet that is used for this demo is shown below.

using System;
using System.Diagnostics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string version = GetAssemblyFileVersion();
            Console.WriteLine(version);
            Console.ReadLine();
        }

        public static string GetAssemblyFileVersion()
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(assembly.Location);
            return fileVersion.FileVersion;
        }
    }
}
    

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...