C# 控制台实用程序应用程序版本的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/212115/
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
Method for Application Version on a Console Utility App
提问by esarjeant
What is the best method for displaying major/minor versions in a C# console application?
在 C# 控制台应用程序中显示主要/次要版本的最佳方法是什么?
The System.Windows.Formsnamespace includes a ProductVersionclass that can be used to display the name/version information set via the Visual Studio project properties (Assembly Information). As such, here is my current mechanism:
该System.Windows.Forms命名空间包含ProductVersion可用于显示通过Visual Studio项目属性(程序集信息)的名称/版本信息集合类。因此,这是我目前的机制:
Console.WriteLine("{0} ({1})",
System.Windows.Forms.Application.ProductName,
System.Windows.Forms.Application.ProductVersion);
Why is this part of Forms? Is this appropriate for a Console application?
为什么是这部分Forms?这是否适用于控制台应用程序?
采纳答案by esarjeant
Assembly.GetExecutingAssembly().GetName().Version
Also, you can still use the class, you just have to reference the containing assembly. It's no biggie.
此外,您仍然可以使用该类,您只需要引用包含程序集。这没什么大不了的。
回答by Joe
Assembly.GetExecutingAssembly().GetName().Versionis not the same as Application.ProductVersion(but may be good enough depending on your environment.
Assembly.GetExecutingAssembly().GetName().Version与Application.ProductVersion(但可能足够好,具体取决于您的环境。
As can be seen with Lutz Reflector, Application.ProductVersionfirst attempts to use the AssemblyInformationalVersionattribute from Assembly.GetEntryAssembly()if it's present, and if GetEntryAssembly()is not null.
从 Lutz Reflector 可以看出,Application.ProductVersion首先尝试使用AssemblyInformationalVersion属性,Assembly.GetEntryAssembly()如果它存在,并且GetEntryAssembly()不为空。
Otherwise it uses the file version of the executable file.
否则,它使用可执行文件的文件版本。
I don't see any reason not to use Application.ProductVersionin a console application.
我看不出有任何理由不在Application.ProductVersion控制台应用程序中使用。

