如何获取程序集文件版本

AssemblyInfo ,有两个程序集版本: spa

  1. AssemblyVersion :指定要赋予属性的部件的版本。
  2. AssemblyFileVersion :指示编译器对Win32文件版本资源使用特定的版本号。 Win32文件版本不须要与程序集的版本号相同。

我能够使用如下代码行获取Assembly Versioncode

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

可是,如何获取Assembly File Versionorm


#1楼

您能够使用My.Application.Info.Version获取程序集版本资源


#2楼

当我要访问应用程序文件版本(在“装配体信息->文件版本”中设置的内容)时,例如说要在表单加载时为其设置标签文本以显示版本,我刚刚使用了get

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

#3楼

三个版本程序集文件产品 。 它们由不一样的功能使用,而且若是您未明确指定它们,则采用不一样的默认值。 编译器

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;

#4楼

用这个: string

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

或这个: 产品

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

#5楼

更新:正如Richard Grimes在我引用的帖子 @Iain和@Dmitry Lobanov中提到的那样,个人回答在理论上是正确的,但在实践中是错误的。 it

正如我从无数书籍等中所记得的那样,虽然使用[assembly: XXXAttribute]设置了这些属性,但它们却被编译器劫持并放置在VERSIONINFO资源中。 io

因为上述缘由,您须要在@Xiaofu的答案中使用该方法,由于从信号中提取信号后,这些属性将被剥离。


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

(来自http://bytes.com/groups/net/420417-assemblyversion属性-如此处所述,若是您要查找其余属性,请将该属性替换为上面的属性)

相关文章
相关标签/搜索