Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if the .Net 5 runtime is installed?

Tags:

c#

.net-5

I installed .NET 5.0 preview SDK and runtime.

How do I detect/determine if the .Net 5 runtime is installed from in C# ?

like image 661
kofifus Avatar asked Aug 17 '20 05:08

kofifus


Video Answer


2 Answers

There are a few things wrong here:

  1. .NET 5 is not a version of .NET Framework, it is the next version of .NET Core (source)
  2. If your app is compiled against .NET 5, and the computer you're trying to run on does not have .NET 5 installed, then your app simply won't launch (think of it like trying to run an application compiled for .NET Framework 4.8 on a computer which only has .NET Framework 3.5 installed)*

And as .NET 5 is the next version of .NET Core, you can easily use the new (in Core 3.0) APIs

var netVersion = System.Environment.Version;
var runtimeVer = System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription;

As mentioned in your original question, you are reading the registry keys for getting the .NET Framework versions (I'm assuming à la so). Well the location for the keys that specify the .NET Core versions installed are located in a different place, namely HKEY_LOCAL_MACHINE\SOFTWARE\dotnet\Setup\InstalledVersions. Here is how you could read them:

const string subkey = @"SOFTWARE\dotnet\Setup\InstalledVersions";
var baseKey = Registry.LocalMachine.OpenSubKey(subkey);
if (baseKey.SubKeyCount == 0)
    return;

foreach (var platformKey in baseKey.GetSubKeyNames())
{
    using (var platform = baseKey.OpenSubKey(platformKey))
    {
        Console.WriteLine($"Platform: {platform.Name.Substring(platform.Name.LastIndexOf("\\") + 1)}");
        if (platform.SubKeyCount == 0)
            continue;

        var sharedHost = platform.OpenSubKey("sharedhost");
        foreach (var version in sharedHost.GetValueNames())
            Console.WriteLine("{0,-8}: {1}", version, sharedHost.GetValue(version));
    }
}

* Expect if you compile your application with self-contained which will bundle the runtime together with your app

like image 178
MindSwipe Avatar answered Sep 23 '22 08:09

MindSwipe


Don't use the accepted answer! .NET 5 Core does not save anything to the registry any longer. I have .NET 5 installed and I have nothing in the registry at SOFTWARE\dotnet.

Also RutimeEnvironment and similar envir-based checks will show invalid results for "self-contained" applications that have the framework bundled into them.

The documented way to check the version is to launch dotnet --info or dotnet --list-runtimes and inspect the results. There's a million and then some ways to launch a console command and check the output.

like image 23
Alex from Jitbit Avatar answered Sep 23 '22 08:09

Alex from Jitbit