I am trying to specify the assembly version in a .NET core (2.0) console application so I can programmatically access it via:
open System.Reflection
let main argv =
printfn "Assembly Version is: %s" <| Assembly.GetEntryAssembly().GetName().Version.ToString()
0
adding a version field to a property group of my .fsproj file e.g.:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Version>1.0.0.1</Version>
</PropertyGroup>
will not change the version that is printed by my test application (it stays at 0.0.0.0).
What works is adding an AssemblyInfo.fs file where is set the AssemblyVersion attribute, but if possible I would like to avoid that and use the .fsproj file. Is this possible?
I also would be happy to just have a pointer to where I can find documentation about .fsproj in general.
There is an issue for this here:
Generate F# assemblyinfo in new fsproj
In short it should work,but it doesn't right now.
.fsproj
filesI believe the format is the same as csproj
as far as MSBuild is concerned
You can find the official documentation about the csproj files here:
https://docs.microsoft.com/en-us/dotnet/core/tools/csproj
To get the assembly's "version", you should note that there are several types of versions:
AssemblyVersion:
Numeric value in the format major.minor.build.revision (for example, 2.4.0.0). The common language runtime uses this value to perform binding operations in strong-named assemblies.
Note: If the AssemblyInformationalVersionAttribute
attribute is not applied to an assembly, the version number specified by the AssemblyVersionAttribute
attribute is used by the Application.ProductVersion
, Application.UserAppDataPath
, and Application.UserAppDataRegistry
properties.
AssemblyFileVersion:
String value specifying the Win32 file version number. This normally defaults to the assembly version.
AssemblyInformationalVersion:
String value specifying version information that is not used by the common language runtime, such as a full product version number.
Note: If this attribute is applied to an assembly, the string it specifies can be obtained at run time by using the Application.ProductVersion
property. The string is also used in the path and registry key provided by the Application.UserAppDataPath
and Application.UserAppDataRegistry
properties.
Application.ProductVersion:
Defines additional version information for an assembly manifest.
You can find out about each one in greater detail on the official Microsoft Docs here -- or you can read about assemblies in general here:
// assembly version
Assembly.GetExecutingAssembly().GetName().Version.ToString();
// assembly version - by path
Assembly.LoadFile('your assembly file').GetName().Version.ToString();
// file version **this is likely what you are seeking**
FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
// product version
FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
Note that the above code snippet was taken from an answer to a similar SO question.
.fsproj
file directlyThere is also the option of just parsing the fsproj
file using XML. This option is intended for programmatically adding references or just checking files -- so it may not apply to your question, but it is here for completeness of the answer.
//using System.Xml.Linq
XDocument.Load(path).Descendants("PropertyGroup").Elements("Version").Single().Value;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With