Paul Duer
Paul Duer

Reputation: 1120

How can I capture the Package Version of a Dot Net Core assembly in startup code?

I know that all Dot Net Core projects get marked with a package version tag that appears in the CSProj file, like this:

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<StartupObject>MY.Program</StartupObject>
<TypeScriptToolsVersion>2.5</TypeScriptToolsVersion>
<AssemblyName>MY.PCA</AssemblyName>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Version>1.2.0</Version>
</PropertyGroup>

Is there a way to get access to that Version number field at Runtime? Namely in the Startup routines found in Startup.cs?

Upvotes: 2

Views: 3398

Answers (2)

wenhx
wenhx

Reputation: 353

Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;

Upvotes: 1

Set
Set

Reputation: 49779

Add Microsoft.Extensions.PlatformAbstractions package as dependency and then use ApplicationEnvironment.ApplicationVersion property to get the version:

// using using Microsoft.Extensions.PlatformAbstractions;

ApplicationEnvironment app = PlatformServices.Default.Application;
string version = app.ApplicationVersion;

Upvotes: 2

Related Questions