Get the .NET assembly's AssemblyInformationalVersion value?

C#.Net

C# Problem Overview


What is the C# syntax for getting the assembly's AssemblyInformationalVersion attribute value at runtime? Example:

[assembly: AssemblyInformationalVersion("1.2.3.4")]

C# Solutions


Solution 1 - C#

using System.Reflection.Assembly  
using System.Diagnostics.FileVersionInfo

// ...

public string GetInformationalVersion(Assembly assembly) {
    return FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion;
}

Solution 2 - C#

var attr = Assembly
    .GetEntryAssembly()
    .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false) 
    as AssemblyInformationalVersionAttribute[];

It's an array of AssemblyInformationalVersionAttribute. It isn't ever null even if there are no attribute of the searched type.

var attr2 = Attribute
    .GetCustomAttribute(
        Assembly.GetEntryAssembly(), 
        typeof(AssemblyInformationalVersionAttribute)) 
    as AssemblyInformationalVersionAttribute;

This can be null if the attribute isn't present.

var attr3 = Attribute
    .GetCustomAttributes(
         Assembly.GetEntryAssembly(), 
         typeof(AssemblyInformationalVersionAttribute)) 
    as AssemblyInformationalVersionAttribute[];

Same as first.

Solution 3 - C#

Using a known type in your application you can simply do this:

using System.Reflection;

public static readonly string ProductVersion = typeof(MyKnownType).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

Of course any process you use to get to the assembly your attribute is applied to is good. Note that this doesn't rely on System.Diagnostics or the WinForm's Application object.

Solution 4 - C#

Even if the question is a bit old:

I propose a different solution that works for me:

Application.ProductVersion

Solution 5 - C#

AssemblyInformationalVersionAttribute attribute = 
   (AssemblyInformationalVersionAttribute)Assembly.GetExecutingAssembly()
   .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false).FirstOrDefault();

if (attribute != null)
     Console.WriteLine(attribute.InformationalVersion);

Solution 6 - C#

public static string GetInformationalVersion() =>
    Assembly
        .GetEntryAssembly()
        .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
        .InformationalVersion;

While my answer is similar to some of the others, I think it has some advantages:

  • It determines the informational version of the entry assembly. That means this code can reside in a library in a bigger project and still get's the version of the "program the user has double clicked" without taking any dependency.
    • If you want to get the version of the assembly that the code resides in (i.e. the library not the main program) you can replace GetEntryAssembly() with GetExecutingAssembly()
  • It doesn't determine the informational version by looking at a file. The I/O operation is unneeded and even impossible in some cases (I'm thinking of some single file packaging methods, AoT variants, software executed from UNC paths, etc).
  • It shares the above two aspects with @xanatos' answer, however I like using the generic extension method GetCustomAttribute<T> better and think this variant is more readable.

See also the Microsoft Docs on GetCustomAttribute<T>(Assembly).

Solution 7 - C#

To complement lance's answer: You can use Application.ResourceAssembly.Location to find out the file path of your assembly. With this it's possible to get the AssemblyInformationalVersion string in just one line

System.Diagnostics.FileVersionInfo.GetVersionInfo(Application.ResourceAssembly.Location).ProductVersion

Solution 8 - C#

Solution 9 - C#

Building off of @Aerthal's answer, if you want a one liner to get the AssemblyInformationalVersionAttribute from a MVC Razor View:

@System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(Zeroarc.Candid.Web.MvcApplication).Assembly.Location).ProductVersion

Solution 10 - C#

A practical approach

Given that retrieving the date from the PE header may not be reliable enough, there is a way to include additional attributes to your AssemblyInfo.cs

[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]

// and this:
[assembly: AssemblyInformationalVersion("1.0.0 (Build Date: 14.07.2020)")]

The string should be readable, because it is visible to the end user. But if you stick to a specific format, it can be parsed with ease and reliability.

Note: We are using a Jenkins build server, which writes version info into the AssemblyInfo.cs along with the date string.

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlanceView Question on Stackoverflow
Solution 1 - C#lanceView Answer on Stackoverflow
Solution 2 - C#xanatosView Answer on Stackoverflow
Solution 3 - C#Robb VandaveerView Answer on Stackoverflow
Solution 4 - C#wollnystView Answer on Stackoverflow
Solution 5 - C#DeCafView Answer on Stackoverflow
Solution 6 - C#Georg JungView Answer on Stackoverflow
Solution 7 - C#AerthelView Answer on Stackoverflow
Solution 8 - C#mrKView Answer on Stackoverflow
Solution 9 - C#jslattsView Answer on Stackoverflow
Solution 10 - C#bytecode77View Answer on Stackoverflow