How can I get the assembly file version

C#.NetAssembliesVersion

C# Problem Overview


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 Assembly Version with the following line of code:

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

But how can I get the Assembly File Version?

C# Solutions


Solution 1 - C#

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;

Solution 2 - C#

There are three versions: assembly, file, and product. 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(); 
string 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;

Solution 3 - C#

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.

Solution 4 - C#

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)

Solution 5 - C#

Use this:

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

Or this:

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

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
QuestionEnyraView Question on Stackoverflow
Solution 1 - C#XiaofuView Answer on Stackoverflow
Solution 2 - C#Check6View Answer on Stackoverflow
Solution 3 - C#syntapView Answer on Stackoverflow
Solution 4 - C#Ruben BartelinkView Answer on Stackoverflow
Solution 5 - C#user6438653View Answer on Stackoverflow