Reputation: 19285
I installed .NET 5.0 preview SDK and runtime.
How do I detect/determine if the .Net 5 runtime is installed from in C# ?
Upvotes: 10
Views: 22392
Reputation: 11
Open visual studio code.
Launch the Terminal from the Menu
Type the following command in the terminal window and read the version.
dotnet --version
Upvotes: 0
Reputation: 60616
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.
Upvotes: 8
Reputation: 170
As of 24 September 2021: I needed to find out what version of Net 5 is installed. I updated my MS VS to the latest version, then created simple console .Net 5 app and got value from RuntimeInformation.FrameworkDescription. It was ".NET 5.0.10"
More information: https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
Upvotes: 1
Reputation: 1146
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}
Upvotes: 2
Reputation: 2759
You can also check by running VS Installer and then going to the Modify section.
Upvotes: -1
Reputation: 7855
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
Upvotes: 8