- 1 year ago
How to find the .NET Core Desktop Runtime is installed in a windows machine
To determine if the .NET Core Desktop Runtime is installed on a Windows machine, you can check the presence of the runtime's registry keys. Here's how you can do it programmatically using C#:
csharpusing Microsoft.Win32;
class Program
{
static void Main()
{
bool isRuntimeInstalled = CheckIfDotNetCoreRuntimeInstalled();
Console.WriteLine(".NET Core Desktop Runtime Installed: " + isRuntimeInstalled);
}
static bool CheckIfDotNetCoreRuntimeInstalled()
{
const string desktopRuntimeKeyName = "Microsoft.NETCore.App";
using (RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\dotnet\Setup\InstalledVersions\x64\Desktop\runtime"))
{
if (ndpKey == null)
return false;
string[] subKeyNames = ndpKey.GetSubKeyNames();
foreach (string subKeyName in subKeyNames)
{
if (subKeyName.Equals(desktopRuntimeKeyName, StringComparison.OrdinalIgnoreCase))
return true;
}
return false;
}
}
}
In this code snippet, we use the Registry
class from the Microsoft.Win32
namespace to access the Windows Registry. We check if the registry key SOFTWARE\dotnet\Setup\InstalledVersions\x64\Desktop\runtime\Microsoft.NETCore.App
exists. If the key is found, it indicates that the .NET Core Desktop Runtime is installed.
Note that this code specifically checks for the x64 version of the .NET Core Desktop Runtime. If you need to check for other architectures or versions, you can modify the registry key accordingly.
When running this code, make sure to have the necessary permissions to access the registry. You may need administrative privileges or adjust security settings accordingly.
Additionally, keep in mind that the exact registry location and key names may vary depending on the .NET Core version and installation settings. Adjust the code accordingly if necessary.