Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Assembly Version from AssemblyInfo.cs

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)

like image 600
Tarik Avatar asked Feb 28 '13 01:02

Tarik


People also ask

How do I find the assembly version?

I can get the Assembly Version with the following line of code: Version version = Assembly. GetEntryAssembly(). GetName().

How do I find the assembly version of a DLL?

If you reference the dll in Visual Studio right click it (in ProjectName/References folder) and select "Properties" you have "Version" and "Runtime Version" there. In File Explorer when you right click the dll file and select properties there is a "File Version" and "Product Version" there.

What is assembly version in C#?

It's the version number used by framework during build and at runtime to locate, link, and load the assemblies. When you add reference to any assembly in your project, it's this version number that gets embedded.

What are the contents of AssemblyInfo CS file?

AssemblyInfo. cs contains information about your assembly, like name, description, version, etc. You can find more details about its content reading the comments that are included in it.


1 Answers

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.

like image 193
SLaks Avatar answered Oct 09 '22 15:10

SLaks