Runtime: Get the .NET Core version number at runtime

Created on 16 Mar 2018  路  16Comments  路  Source: dotnet/runtime

@IvanFarkas commented on Sat Jan 27 2018

Is there a reliable way to get the .NET Core version number at runtime that works on Nano Server and Ubuntu Docker containers as well, please?
I've seen some hacks deriving it from folder structure that breaks when the app is deployed onto Nano Server and Ubuntu Docker container images.

area-System.Runtime question

Most helpful comment

Please add a simple way to get the runtime version. No, not IsRuntimeVersion(x), just public System.Version RuntimeVersion and maybe a human-readable string with more details (if any). Use case: troubleshooting. I have several versions of the runtime and the SDK instealled and I want to know, not guess, what runtime version my application is actually running on (as opposed to should run on).

All 16 comments

@IvanFarkas I'm curious for what purpose you need the version at runtime?

:-)
I need to know.

@IvanFarkas - well, there's a difference between "I'd like to know to log it on program startup", and "I'd like to know for feature detection/bug workarounds"....

Personally, considering that I'm using various versions of .Net Framework and .Net Core (including previews) and that I'm sometimes investigating behaviors that differ depending on which framework I'm using, I'd like to be able to print what framework I'm running on, so that I could easily write code that does something like:

> dotnet run -f net47
.Net Framework 4.7.1: Everything's fine
> dotnet run -f netcoreapp1.0
.Net Core 1.0: Everything's fine
> dotnet run -f netcoreapp2.0
.Net Core 2.0: BUG!

To do this today, I copied this code from BenchmarkDotNet.

@Clockwork-Muse I like contributors answering questions constructively, instead of interrogating about the motivation.
This is how we roll in the better part of Europe.

@IvanFarkas Motivation is important, because it can affect the shape of the API. For example, should the API be bool isNetCore20 = RuntimeInformation.IsFramework(FrameworkVersion.NetCore20);, or string frameworkVersion = RuntimeInformation.GetFrameworkVersion();?

Run time statistics is basic to me and the lack of it is a serious oversight!
I can just use the Docker env, reporting .net runtime info if it鈥檚 too much trouble implementing this obviously lacking piece.

We have a property (RuntimeInformation.FrameworkDescription) but it is missing the framework version.
If it is not a huge breaking change, then the version can be added to the existing description string, otherwise a new Property static string FrameworkVersion may even suffice the requirement (and IsFramework(<enum FrameworkVersion>) might not be necessary). Otherwise we can have both, property and the method, just like OSDescription and IsOSPlatform() are present in the same class.

@IvanFarkas - like @svick says, knowing motivation/intended uses changes what answer is likely to be given.

For an example I run into all the time, people on StackOverflow are always asking, "how do I get month and year from a date in SQL?". My first question is going to be "are you using the information for filtering (in the WHERE clause), or for display (in the SELECT clause)?". Because if it's for display, a function that retrieves year/month is fine. If it's in the WHERE clause, you almost certainly want to _avoid_ those same functions, because doing so can invalidate the use of an index (and instead should be constructing the limits of the range).

I am running

  • different versions of .NET Core runtime
  • cross-compiled with different versions of .NET Core SDK
  • on different versions of Linux distro
  • inside different versions of Docker.

At runtime, I'd like to know

  • what am I running on
  • what am I running
  • and where it came from.

Thank you very much for your help in advance.

Please add a simple way to get the runtime version. No, not IsRuntimeVersion(x), just public System.Version RuntimeVersion and maybe a human-readable string with more details (if any). Use case: troubleshooting. I have several versions of the runtime and the SDK instealled and I want to know, not guess, what runtime version my application is actually running on (as opposed to should run on).

I'm now trying out self-contained deployment and have no idea how to get the .NET Core version included with the deployed application - either at runtime or by inspecting the binaries.

Another use case is benchmarking: I need to know what .NET Core runtime version I'm running on to see if it's the same as on the server I'm comparing against.

This use case: security updates - how do I know when I need to rebuild and re-deploy my self-contained deployment application?

I am using the following method:
First Get the RunTime Directory: System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
Next, Use Regex to pick up the Runtime Info and Version

@doublnt That doesn't help for self-contained deployments. GetRuntimeDirectory() then returns the directory containing the EXE (which is correct - that's where the runtime is).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chunseoklee picture chunseoklee  路  3Comments

EgorBo picture EgorBo  路  3Comments

GitAntoinee picture GitAntoinee  路  3Comments

jkotas picture jkotas  路  3Comments

jamesqo picture jamesqo  路  3Comments