C# 如何获取程序集文件版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/909555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:35:27  来源:igfitidea点击:

How can I get the assembly file version

c#.netassembliesversion

提问by Enyra

In AssemblyInfothere are two assembly versions:

AssemblyInfo有两个集版本:

  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.
  1. AssemblyVersion: 指定要归因的程序集的版本。
  2. AssemblyFileVersion: 指示编译器使用 Win32 文件版本资源的特定版本号。Win32 文件版本不需要与程序集的版本号相同。

I can get the Assembly Versionwith the following line of code:

我可以Assembly Version使用以下代码行获取:

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

But how can I get the Assembly File Version?

但是我怎样才能得到Assembly File Version

采纳答案by Xiaofu

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;

回答by Ruben Bartelink

UPDATE: As mentioned by Richard Grimes in my cited post, @Iain and @Dmitry Lobanov, my answer is right in theory but wrong in practice.

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

As I should have remembered from countless books, etc., while one sets these properties using the [assembly: XXXAttribute], they get highHymaned by the compiler and placed into the VERSIONINFOresource.

正如我应该从无数书籍等中记住的那样,当人们使用 设置这些属性时[assembly: XXXAttribute],它们会被编译器劫持并放入VERSIONINFO资源中。

For the above reason, you need to use the approach in @Xiaofu's answeras the attributes are stripped after the signal has been extracted from them.

由于上述原因,您需要使用@Xiaofu's answer 中的方法,因为在从属性中提取信号后将剥离属性。



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)

(来自http://bytes.com/groups/net/420417-assemblyversionattribute- 如上所述,如果您正在寻找不同的属性,请将其替换为上述属性)

回答by Check6

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;

回答by syntap

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;

回答by syntap

Use this:

用这个:

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

Or this:

或这个:

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