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

Viewed 16903

I installed .NET 5.0 preview SDK and runtime.

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

6 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

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.

I am making an installer for a dotnet product, and one of my prerequisites is to install the dotnet 5.n.n runtime. I am testing all scenarios in few VMs, and here is what inconsistency I have found.

On the VM, the first time I started my test, there was no MS-NET product at all, so it was easy to determine this runtime absence through the registry key (HKLM/software/dotnet/...). However, when I install the runtime then uninstall it, the registry key did not get deleted (it was deleted on another VM by the way), so dotnet runtime check through the registry key returned true, but in fact, the runtime was not installed in the machine, I did uninstall it minute before. This was my 1st check.

So, what I did next (the 2nd check)? I did check the dotnet physical existence in the file system. That would be C:\Program Files\dotnet\shared or C:\Program Files (x86)\dotnet\shared location based on 32/64 bit installation. Note that these folders may contain one or more subfolder, and one that I was interested is this one: Microsoft.NETCore.App -> holds core + not 5.n.n runtimes...

And the 3rd check is the registry installed/uninstalled key for the runtime: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall{guid}

Open visual studio code.

Launch the Terminal from the Menu

Type the following command in the terminal window and read the version.

dotnet --version

You can also check by running VS Installer and then going to the Modify section.

enter image description here

Related