"dotnet --list-runtimes" just enumerates subfolders from "C:\Program Files\dotnet\shared"
with FindFirstFileEx and so on
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
what is the best way how to check all installed .NET runtime versions from C# code?
Is there some registry key I could read? e.g. SOFTWARE\WOW6432Node\dotnet\Setup\InstalledVersions\x86\hostfxr
Or do I need to call "dotnet --list-runtimes" via System.Diagnostics.Process.Start().
Thank You For Answer.
Jan
"dotnet --list-runtimes" just enumerates subfolders from "C:\Program Files\dotnet\shared"
with FindFirstFileEx and so on
I'd use dotnet --list-runtimes
like this:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public static class DotNetHelper
{
public static List<string> ListRuntimes()
{
var dotnet = Process.Start(new ProcessStartInfo("dotnet", "--list-runtimes")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
});
dotnet.WaitForExit();
string result = dotnet.StandardOutput.ReadToEnd().TrimEnd();
string errors = dotnet.StandardError.ReadToEnd().TrimEnd();
if (!string.IsNullOrEmpty(errors))
throw new Exception(errors);
return result.Split(new string[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries).ToList();
}
}
The one with checking the directory works as well, but I assume it's more implementation details; and relying on the CLI makes more sense IMO.