我希望在运行时检索应用程序版本(本质上是解决方案文件中的<ReleaseVersion>
属性)。如何通过代码访问此内容?
答案 0 :(得分:6)
在.NET中设置应用程序版本的标准方法(因此可能是MONO)是使用AssemblyVersion
属性。这通常在AssemblyInfo.cs文件中指定,但可以在其他文件中指定,如下所示。
要在运行时获取版本,可以使用AssemblyName.Version property。以下是给定链接的代码的略微修改版本:
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);
}
}
编译并运行时,会生成:
当前正在执行的程序集的版本是:1.1.0.0
上述内容在.NET(Visual Studio 2010)和Mono 3.0(使用mcs
从命令行编译而不是Mono Develop)上进行了测试。
答案 1 :(得分:0)
您是否尝试过使用此论坛中的建议 - &gt; get current version
答案 2 :(得分:0)
这是一个丑陋的hack,但是您可以利用BeforeBuild
目标将该属性写入文件,然后将其立即作为嵌入式资源包括在内。在您的* .csproj文件中:
<ItemGroup>
<EmbeddedResource Include="release-version.txt" />
</ItemGroup>
<!-- .... -->
<Target Name="BeforeBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="echo $(ReleaseVersion) >release-version.txt" />
</Target>
...。然后在您的C♯代码中,您可以创建如下属性:
public static string Version {
get {
using (StreamReader resourceReader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SBRL.GlidingSquirrel.release-version.txt")))
{
return resourceReader.ReadToEnd().Trim();
}
}
}
这应该在所有主要操作系统上都有效。不要忘记:
using System.Reflection;
,请添加到文件顶部release-version.txt
文件添加到版本控件的忽略列表(例如.gitignore
等)