我知道对于旧版本的.NET,您可以通过以下方法确定是否安装了给定的版本
https://support.microsoft.com/en-us/kb/318785
是否有官方的方法来确定是否安装了。net Core ?
(我不是说SDK,我想检查一个没有SDK的服务器,以确定它是否安装了DotNetCore.1.0.0-WindowsHosting.exe)
我能看到
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NET Cross-Platform Runtime Environment\.NET Framework 4.6\Win\v1-rc1
在我的windows 7机器上使用1.0.11123.0版本#,但我在windows 10机器上没有看到相同的东西。
我来这里是为了寻找一种程序化的方法来确定这一点;虽然这个问题有很多好的答案,但似乎没有一个是程序化的。
我用c#做了一个小文件,解析dotnet.exe——list-runtimes的输出,可以很容易地适应你的需要。它使用CliWrap nuget包;你可能不需要它,但我已经在我的项目中使用了它,因为它使命令行处理更容易满足我的需求。
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CliWrap;
using CliWrap.EventStream;
// License: Do whatever you want with this. This is what my project uses,
// I make no guarantees it works or will work in the future
// THIS IS ONLY FOR .NET CORE DETECTION (no .NET framework!)
// Requires CliWrap https://github.com/Tyrrrz/CliWrap
namespace DotnetHelper
{
/// <summary>
/// Class that can determine if a version of .NET Core is installed
/// </summary>
public class DotNetRuntimeVersionDetector
{
/// <summary>
/// This is very windows specific
/// </summary>
/// <param name="desktopVersionsOnly">If it needs to filter to Windows Desktop versions only (WPF/Winforms).</param>
/// <returns>List of versions matching the specified version</returns>
public static async Task<Version[]> GetInstalledRuntimeVersions(bool desktopVersion)
{
// No validation. Make sure exit code is checked in the calling process.
var cmd = Cli.Wrap(@"dotnet.exe").WithArguments(@"--list-runtimes").WithValidation(CommandResultValidation.None);
var runtimes = new List<Version>();
await foreach (var cmdEvent in cmd.ListenAsync())
{
switch (cmdEvent)
{
case StartedCommandEvent started:
break;
case StandardOutputCommandEvent stdOut:
if (string.IsNullOrWhiteSpace(stdOut.Text))
{
continue;
}
if (stdOut.Text.StartsWith(@"Microsoft.NETCore.App") && !desktopVersion)
{
runtimes.Add(parseVersion(stdOut.Text));
}
else if (stdOut.Text.StartsWith(@"Microsoft.WindowsDesktop.App") && desktopVersion)
{
runtimes.Add(parseVersion(stdOut.Text));
}
break;
case StandardErrorCommandEvent stdErr:
break;
case ExitedCommandEvent exited:
break;
}
}
return runtimes.ToArray();
}
private static Version parseVersion(string stdOutText)
{
var split = stdOutText.Split(' ');
return Version.Parse(split[1]); // 0 = SDK name, 1 = version, 2+ = path parts
}
}
}