Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting .net core 2.0

In a dotnet core 2.0 console application, the output of:

Console.WriteLine("Hello World from "+ System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription);

Is a rather unexpected value:

Hello World from .NET Core 4.6.00001.0

Is there any way to detect .net core 2.0 or later, versus a pre-2.0 .net core platform programmatically? I realize that you probably shouldn't do this in most cases. But in the odd cases where you do need to do this, how would you do this?

like image 478
Warren P Avatar asked Aug 17 '17 21:08

Warren P


People also ask

How do I know if .NET Framework 2.0 is installed?

Check for the "HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2. 0.50727" registry key is present or not. If you need to verify a registry entry then check for the "OCM" (REG_DWORD) entry.

How do I check my current .NET core version?

You can see both the SDK versions and runtime versions with the command dotnet --info .

How do I know if .NET 4.6 2 is installed?

Detect .NET Framework 4.5 and later versions. The version of .NET Framework (4.5 and later) installed on a machine is listed in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full. If the Full subkey is missing, then .NET Framework 4.5 or above isn't installed.


1 Answers

You can use preprocessor symbols that are predefined for you. For example:

var isNetCore2 = false;

#if NETCOREAPP2_0
    isNetCore2 = true;
#endif

Console.WriteLine($"Is this .Net Core 2: {isNetCore2}"); 
like image 113
DavidG Avatar answered Oct 01 '22 13:10

DavidG