Getting Assembly Version from AssemblyInfo.cs
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.
Tarik
I am a software engineer with interests in different technologies.
Updated on February 08, 2020Comments
-
Tarik about 4 years
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 fromAssemblyInfo.cs
on our UI. I've found the following solution in SO from C# AssemblyFileVersion usage within a programusing 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 fromAssemblyInfo.cs
file that is residing in the Web Project all the time? (which is what I want) -
Tarik about 11 yearsThen can you show me how to get Assembly Version info from
AssemblyInfo.cs
of the Web Application all the time? I just want to ensure that the field I need is coming from there. -
SLaks about 11 years@Tarik: Use any type from that project.
-
CrazyIvan1974 almost 7 yearsThis answer led me to the following (VB) code:
New ApplicationServices.AssemblyInfo(Reflection.Assembly.GetExecutingAssembly())
-
starbeamrainbowlabs almost 6 yearsThis gets the version from the source file, yes - but the source code file won't be available when a program is running, say, on a customer's computer.
-
John Lord over 4 yearsnobody is making you point at a source file. You could reference a dll or html page.
-
Souza over 4 yearsVery simple. Thanks. typeof(SomeType).Assembly
-
Spencer Sullivan about 3 years@SLaks - You led me there and I gave you the upvote. However, the answer could be worded better to give it to the viewer without having to do extra work to find the answer. For me the answer was: System.Reflection.Assembly theAssembly = System.Reflection.Assembly.GetExecutingAssembly(); // and the actual Version value is: var AssemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();