I installed .NET 5.0 preview SDK and runtime.
How do I detect/determine if the .Net 5 runtime is installed from in C# ?
There are a few things wrong here:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With