Mono/MonoDevelop:在运行时获取解决方案版本 [英] Mono / MonoDevelop: Get solution version at runtime
问题描述
我正在寻找在运行时检索应用程序版本(本质上是解决方案文件中的<ReleaseVersion>
属性).一个人如何通过代码访问它?
I am looking to retrieve the application version (essentially the <ReleaseVersion>
property in the solution file) at runtime. How does one access this via code?
推荐答案
在.NET中(因此可能是MONO)设置应用程序版本的标准方法是使用AssemblyVersion
属性.通常,这是在AssemblyInfo.cs文件中指定的,但也可以在其他文件中指定,如下所示.
The standard way of setting the application version in .NET (and therefore presumably MONO) is to use the AssemblyVersion
attribute. This is normally specified in the AssemblyInfo.cs file, but can be specified in other files, as is shown below.
To get the version at runtime, you can use the AssemblyName.Version property. The following is a slightly modified version of the code from the given link:
using System;
using System.Reflection;
[assembly:AssemblyVersion("1.1.0.0")]
class Example
{
static void Main()
{
Console.WriteLine("The version of the currently executing assembly is: {0}",
Assembly.GetExecutingAssembly().GetName().Version);
}
}
编译并运行时,它会产生以下内容:
When compiled and run, it produces this:
当前正在执行的程序集的版本为:1.1.0.0
The version of the currently executing assembly is: 1.1.0.0
以上内容已在.NET(Visual Studio 2010)和Mono 3.0(从命令行使用mcs
而不是Mono Develop编译)上进行了测试.
The above was tested on both .NET (Visual Studio 2010), and Mono 3.0 (compiled using mcs
from the command line, not Mono Develop).
这篇关于Mono/MonoDevelop:在运行时获取解决方案版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!