问题描述
从Blah.dll的AssemblyInfo.cs提供以下代码段:
Given this snippet from Blah.dll's AssemblyInfo.cs:
[assembly: AssemblyVersion("3.3.3.3")]
[assembly: AssemblyFileVersion("2.2.2.2")]
然后在单独的.exe中:
And then in a separate .exe:
var fileInfo = FileVersionInfo.GetVersionInfo("/path/to/Blah.dll");
fileInfo.ProductVersion == fileInfo.FileVersion == true;
其他SO问题显示ProductVersion是正确的",好奇我的使用方式是否有些奇怪.
Other SO questions show ProductVersion being "correct", curious if there is something odd about how I'm using it.
ProductVersion不应为"3.3.3.3",FileVersion是否应为"2.2.2.2"吗?是什么导致它将两个属性都报告为AssemblyFileVersion?
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?
推荐答案
我最初找到了答案此处.为了方便参考,我在重复这些细节.
I found the answer originally here. I'm repeating the details for ease of reference.
AssemblyInfo.cs文件中可以包含三个版本":
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
,则默认为AssemblyFileVersion
.同样,如果未同时指定AssemblyInformationalVersion
和 AssemblyFileVersion
,则默认为AssemblyVersion
.
AssemblyInformationalVersion
defaults to AssemblyFileVersion
if it is not specified. Likewise, AssemblyInformationalVersion
and AssemblyFileVersion
default to AssemblyVersion
if both are not specified.
在您的示例中,AssemblyInfo.cs文件不包含AssemblyInformationalVersion
,因此它默认为AssemblyFileVersion
的值.正如您将在下面看到的,AssemblyInformationalVersion
映射到FileVersionInfo.ProductVersion
属性,这说明了测试为何返回true的原因.
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.
显然,有两个令人沮丧的方面.首先,无法(据我所知)从Visual Studio设置AssemblyInformationalVersion
.您必须直接修改AssemblyInfo.cs文件以包括此属性.其次,AssemblyInformationalVersion
映射到FileVersionInfo.ProductVersion
属性,这是不直观的.该属性应更正确地命名为AssemblyProductVersion
.显然,标题也是描述,等等.
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
对于AssemblyVersion
,请使用以下命令:
In the case of AssemblyVersion
, use this:
string ver = Assembly.GetExecutingAssembly().GetName().Version.ToString();
这篇关于FileVersionInfo和AssemblyInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!