问题
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?
回答1:
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:
The version of the currently executing assembly is: 1.1.0.0
The above was tested on both .NET (Visual Studio 2010), and Mono 3.0 (compiled using mcs
from the command line, not Mono Develop).
回答2:
have you tried using the suggestions in this forum --> get current version
回答3:
It's an ugly hack, but you can utilise the BeforeBuild
target to write that property to a file, which you then promptly include as an embedded resource. In your *.csproj file:
<ItemGroup>
<EmbeddedResource Include="release-version.txt" />
</ItemGroup>
<!-- .... -->
<Target Name="BeforeBuild">
<Exec WorkingDirectory="$(ProjectDir)" Command="echo $(ReleaseVersion) >release-version.txt" />
</Target>
....then in your C♯ code, you could create a property like this:
public static string Version {
get {
using (StreamReader resourceReader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("SBRL.GlidingSquirrel.release-version.txt")))
{
return resourceReader.ReadToEnd().Trim();
}
}
}
This should work on all major operating systems. Don't forget:
- To add
using System.Reflection;
to the top of your file if you haven't already - To add the
release-version.txt
file to your version control's ignore list (e.g..gitignore
, etc.)
来源:https://stackoverflow.com/questions/15350577/mono-monodevelop-get-solution-version-at-runtime