Getting Assembly Version from AssemblyInfo.cs

Tarik picture Tarik · Feb 28, 2013 · Viewed 48k times · Source

We have an AssemblyInfo.cs file in our Web Application but we do have other projects in the same solution. They are mostly class libraries. I am told to display Assembly Version from AssemblyInfo.cs on our UI. I've found the following solution in SO from C# AssemblyFileVersion usage within a program

using System.Reflection;
Version version = Assembly.GetExecutingAssembly().GetName().Version;

or

using System.Reflection;
using System.IO;

FileVersionInfo fv = System.Diagnostics.FileVersionInfo.GetVersionInfo
                               (Assembly.GetExecutingAssembly().Location);

Console.WriteLine("AssemblyVersion : {0}", 
         Assembly.GetExecutingAssembly().GetName().Version.ToString());

Console.WriteLine ("AssemblyFileVersion : {0}" , 
         fv.FileVersion.ToString ());

But this confuses me little bit. It says GetExecutingAssembly() and what if it is running on another assembly from other class libraries? Will those codes fetch from AssemblyInfo.cs file that is residing in the Web Project all the time? (which is what I want)

Answer

SLaks picture SLaks · Feb 28, 2013

As the documentation states, Assembly.GetExecutingAssembly() gets the assembly that the calling code was compiled inside of.

If you want to be more explicit (and faster), you can write typeof(SomeType).Assembly, where SomeType is any type in the project you're looking for.