获取.NET程序集的AssemblyInformationalVersion值?


74

AssemblyInformationalVersion在运行时获取程序集的属性值的C#语法是什么?例:

[assembly: AssemblyInformationalVersion("1.2.3.4")]

Answers:


72
using System.Reflection.Assembly  
using System.Diagnostics.FileVersionInfo

// ...

public string GetInformationalVersion(Assembly assembly) {
    return FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion;
}

8
请注意,如果尚未从文件或UNC加载程序集,则此代码不起作用。如果装配体嵌入到另一个装配体中(通常是在混淆装配体时),或者由于某些其他原因而使用Assembly.Load(byte[])
larsmoa

2
如果您已mkbundle申请Mono的申请,也无法正常工作
Cocowalla 2014年

41
var attr = Assembly
    .GetEntryAssembly()
    .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false) 
    as AssemblyInformationalVersionAttribute[];

这是的数组AssemblyInformationalVersionAttribute。即使没有搜索类型的属性,也永远不会为null。

var attr2 = Attribute
    .GetCustomAttribute(
        Assembly.GetEntryAssembly(), 
        typeof(AssemblyInformationalVersionAttribute)) 
    as AssemblyInformationalVersionAttribute;

如果属性不存在,则可以为null。

var attr3 = Attribute
    .GetCustomAttributes(
         Assembly.GetEntryAssembly(), 
         typeof(AssemblyInformationalVersionAttribute)) 
    as AssemblyInformationalVersionAttribute[];

与第一个相同。


1
+1; 但是您可以使用GetCustomAttribute代替,GetCustomAttributes如果您知道只有一个属性。
vcsjones 2011年

3
@vcsjones仅使用的静态方法Attribute,而不使用Assembly
xanatos

1
在某些情况下,Assembly.GetEntryAssembly()应以Assembly.GetExecutingAssembly()或代替Assembly.GetCallingAssembly()。通常,如果程序集是插件,则这是必需的-在这种情况下,GetEntryAssembly()它将返回主机应用程序程序集。
larsmoa

20

在应用程序中使用已知类型,您可以简单地执行以下操作:

using System.Reflection;

public static readonly string ProductVersion = typeof(MyKnownType).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

当然,用于获得应用属性的程序集的任何过程都是好的。请注意,这不依赖System.DiagnosticsWinForm的Application对象。


12

即使问题有点老了:

我提出了一个适合我的解决方案:

Application.ProductVersion

7
我很难在WPF中找到它,它是Winforms Application类。:-)
Wouter

1
有什么理由不使用此变体吗?与此相比,所有其他答案看起来都很复杂。
Rev1.0

2
@ Rev1.0,如果您不另外使用WinForms,则将大于5兆的内存加载到RAM中只是为了程序员的方便。
tm1

9
AssemblyInformationalVersionAttribute attribute = 
   (AssemblyInformationalVersionAttribute)Assembly.GetExecutingAssembly()
   .GetCustomAttributes(typeof(AssemblyInformationalVersionAttribute), false).FirstOrDefault();

if (attribute != null)
     Console.WriteLine(attribute.InformationalVersion);

+1,因为这也适用于Silverlight或Xbox Lakeview!
ahilsend 2013年

6

补充lance的答案:您可以Application.ResourceAssembly.Location用来查找程序集的文件路径。这样就可以在一行中获得AssemblyInformationalVersion字符串

System.Diagnostics.FileVersionInfo.GetVersionInfo(Application.ResourceAssembly.Location).ProductVersion


0

如果您希望一个衬板从MVC剃刀视图中获取AssemblyInformationalVersionAttribute,请以@Aerthal的答案为基础:

@System.Diagnostics.FileVersionInfo.GetVersionInfo(typeof(Zeroarc.Candid.Web.MvcApplication).Assembly.Location).ProductVersion

0

实用的方法

鉴于从PE标头中检索日期可能不够可靠,因此有一种方法可以将其他属性包含到您的 AssemblyInfo.cs

[assembly: AssemblyVersion("1.0.0")]
[assembly: AssemblyFileVersion("1.0.0")]

// and this:
[assembly: AssemblyInformationalVersion("1.0.0 (Build Date: 14.07.2020)")]

该字符串应可读,因为它对最终用户可见。但是,如果您坚持使用特定格式,则可以轻松,可靠地对其进行解析

注意:我们正在使用Jenkins构建服务器,该服务器将版本信息AssemblyInfo.cs与日期字符串一起写入。

在此处输入图片说明


0
public static string GetInformationalVersion() =>
    Assembly
        .GetEntryAssembly()
        .GetCustomAttribute<AssemblyInformationalVersionAttribute>()
        .InformationalVersion;

虽然我的回答与其他一些回答相似,但我认为它具有一些优点:

  • 它确定输入程序集的信息版本。这意味着该代码可以驻留在更大项目中的库中,并且无需任何依赖即可获得“用户双击的程序”的版本。
    • 如果要获取代码所在的程序集的版本(即库而不是主程序),则可以替换GetEntryAssembly()GetExecutingAssembly()
  • 它不能通过查看文件来确定信息版本。I / O操作是不必要的,甚至在某些情况下甚至是不可能的(我正在考虑某些单文件打包方法,AoT变体,从UNC路径执行的软件等)。
  • 它与@xanatos的答案具有上述两个方面,但是我喜欢GetCustomAttribute<T>更好地使用通用扩展方法,并且认为此变体更具可读性。

另请参阅上的Microsoft文档GetCustomAttribute<T>(Assembly)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.