32

Given this snippet from Blah.dll's AssemblyInfo.cs:

[assembly: AssemblyVersion("3.3.3.3")]
[assembly: AssemblyFileVersion("2.2.2.2")]

And then in a separate .exe:

var fileInfo = FileVersionInfo.GetVersionInfo("/path/to/Blah.dll");
fileInfo.ProductVersion == fileInfo.FileVersion == true;

Other SO questions show ProductVersion being "correct", curious if there is something odd about how I'm using it.

Shouldn't ProductVersion be "3.3.3.3" and FileVersion be "2.2.2.2"? What would cause it to report both properties as AssemblyFileVersion?

wonea
  • 4,783
  • 17
  • 86
  • 139
ecoffey
  • 3,423
  • 4
  • 23
  • 22

1 Answers1

60

I found the answer originally here. I'm repeating the details for ease of reference.

There are three 'versions' that can be included in the AssemblyInfo.cs file:

[assembly: AssemblyVersion("1.1.1.1")]
[assembly: AssemblyInformationalVersion("2.2.2.2")]
[assembly: AssemblyFileVersion("3.3.3.3")]

AssemblyInformationalVersion defaults to AssemblyFileVersion if it is not specified. Likewise, AssemblyInformationalVersion and AssemblyFileVersion default to AssemblyVersion if both are not specified.

In your example, the AssemblyInfo.cs file did not include AssemblyInformationalVersion, so it defaults to the value of AssemblyFileVersion. As you will see below, AssemblyInformationalVersion maps to the FileVersionInfo.ProductVersion property, which explains why the test returns true.

Obviously, there are a couple of frustrating aspects to this. First, there is no way (that I know of) to set the AssemblyInformationalVersion from Visual Studio. You have to modify the AssemblyInfo.cs file directly to include this attribute. Second, AssemblyInformationalVersion maps to the FileVersionInfo.ProductVersion property, which is non-intuitive. The attribute should more properly be named AssemblyProductVersion. And apparently, a title is also a description, etc.

That said, how do we retrieve these (and related) values in code? Like this:

AssemblyFileVersion          => System.Diagnostics.FileVersionInfo.FileVersion
AssemblyInformationalVersion => System.Diagnostics.FileVersionInfo.ProductVersion
AssemblyVersion              => System.Reflection.Assembly.Version

/// others...
AssemblyTitle                => System.Diagnostics.FileVersionInfo.FileDescription
AssemblyDescription          => System.Diagnostics.FileVersionInfo.Comments
AssemblyProduct              => System.Diagnostics.FileVersionInfo.ProductName
AssemblyCompany              => System.Diagnostics.FileVersionInfo.CompanyName
AssemblyCopyright            => System.Diagnostics.FileVersionInfo.LegalCopyright
AssemblyTrademark            => System.Diagnostics.FileVersionInfo.LegalTrademarks

In the case of AssemblyVersion, use this:

string ver = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
Matt Davis
  • 45,297
  • 16
  • 93
  • 124
  • I accepted the answer because that is the correct explanation, but I was hoping for a solution that didn't involve Loading (or even ReflectionOnly Loading) the assembly. But thanks for hunting down at least that! – ecoffey Dec 17 '10 at 16:57
  • 1
    If you want to be able to read Assembly version from a DLL without loading or reflection-only-loading, then you can read the meta-data purely as a data source, into an object model using the Microsoft Common Compiler Infrastructure (CCI) library. You'll be interested in the MetaDataReaderHost. Its fast, and doesnt 'CLR load' the DLL in any way - giving the advantage of being able to read meta data from DLLs that are compiled against newer versions of the runtime than your application, thereby future proofing your application. http://ccimetadata.codeplex.com/ – Adam Jun 20 '11 at 02:56
  • 3
    @ecoffey: "System.Diagnostics.FileVersionInfo.GetVersionInfo(fullPath)" doesn't load the assembly, and you can call "FileVersion" or "ProductVersion. – user276648 Dec 12 '12 at 05:52
  • 1
    @ecoffey Very ancient now, but pending Matt's approval of my edits, I've updated his list here to more completely show the mapping. – Glenn Slayden Sep 10 '18 at 02:24
  • @user276648: Your comment regarding `ProductVersion` is just not true, this (at least now) retrieves `[assembly: AssemblyInformationalVersion("...")]`. – vonludi Mar 10 '20 at 14:19