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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...