Enyra
Enyra

Reputation: 17992

How can I get the assembly file version

In AssemblyInfo there are two assembly versions:

  1. AssemblyVersion: Specify the version of the assembly being attributed.
  2. AssemblyFileVersion: Instructs a compiler to use a specific version number for the Win32 file version resource. The Win32 file version is not required to be the same as the assembly's version number.

I can get the AssemblyVersion with the following line of code:

Version version = Assembly.GetEntryAssembly().GetName().Version;

But how can I get the AssemblyFileVersion?

Upvotes: 901

Views: 629554

Answers (6)

Check6
Check6

Reputation: 3267

There are three versions: assembly, file, and product (aka Assembly Informational Version). They are used by different features and take on different default values if you don't explicit specify them.

string assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 
assemblyVersion = Assembly.LoadFile("your assembly file").GetName().Version.ToString(); 
string fileVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; 
string productVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;

Upvotes: 325

Georg Jung
Georg Jung

Reputation: 1167

Cross-posting my answer from a very similar question, because I was googling for my own answer and ended up here. I think the following approach has some advantages compared to the others shown here. Original answer below.


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).

Upvotes: 4

Ruben Bartelink
Ruben Bartelink

Reputation: 61885

UPDATE: As mentioned by Richard Grimes in my cited post, @Iain and @Dmitry Lobanov, my answer is right in theory but wrong in practice.

As I should have remembered from countless books, etc., while one sets these properties using the [assembly: XXXAttribute], they get highjacked by the compiler and placed into the VERSIONINFO resource.

For the above reason, you need to use the approach in @Xiaofu's answer as the attributes are stripped after the signal has been extracted from them.


public static string GetProductVersion()
{
  var attribute = (AssemblyVersionAttribute)Assembly
    .GetExecutingAssembly()
    .GetCustomAttributes( typeof(AssemblyVersionAttribute), true )
    .Single();
   return attribute.InformationalVersion;
}

(From http://bytes.com/groups/net/420417-assemblyversionattribute - as noted there, if you're looking for a different attribute, substitute that into the above)

Upvotes: 28

syntap
syntap

Reputation: 1091

When I want to access the application file version (what is set in Assembly Information -> File version), say to set a label's text to it on form load to display the version, I have just used

versionlabel.Text = "Version " + Application.ProductVersion;

This approach requires a reference to System.Windows.Forms.

Upvotes: 94

Xiaofu
Xiaofu

Reputation: 15899

See my comment above asking for clarification on what you really want. Hopefully this is it:

System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.FileVersionInfo fvi = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;

Upvotes: 1036

user6438653
user6438653

Reputation:

Use this:

((AssemblyFileVersionAttribute)Attribute.GetCustomAttribute(
    Assembly.GetExecutingAssembly(), 
    typeof(AssemblyFileVersionAttribute), false)
).Version;

Or this:

new Version(System.Windows.Forms.Application.ProductVersion);

Upvotes: 15

Related Questions