如何确定是否安装了 .NET Core

How to determine if .NET Core is installed

我知道对于旧版本的 .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 机器上看不到相同的东西。

您可以查看dotnet.exe是否可用:

where dotnet

然后您可以查看版本:

dotnet --version

更新:现在有一个更好的方法来做到这一点,这在许多其他答案中都有很好的解释:

dotnet --info

确定 .NET Core 是否安装在 Windows 上的虚拟方法之一是:

  • Windows + R
  • 类型cmd
  • 在命令提示符下,键入 dotnet --version

如果安装了.NET Core,我们在上面的步骤中应该不会出现任何错误。

好问题,答案并不简单。没有"show me all .net core versions"命令,但还有希望。

编辑:

我不确定它是何时添加的,但 info 命令现在在其输出中包含此信息。它将打印出已安装的运行时和 SDK,以及一些其他信息:

dotnet --info

如果您只想查看 SDK:dotnet --list-sdks

如果您只想查看已安装的运行时:dotnet --list-runtimes

我正在使用 Windows,但我猜这对 Mac 或 Linux 也适用于当前版本。

另外,您可以参考.NET Core Download Archive帮助您破解SDK版本。


旧信息: 此点以下的所有内容都是旧信息,不太相关,但可能仍然有用。

查看已安装的运行时

在 Windows 资源管理器

中打开 C:\Program Files\dotnet\shared\Microsoft.NETCore.App

查看已安装的 SDK:

在 Windows 资源管理器

中打开 C:\Program Files\dotnet\sdk

(位置来源:A developer's blog


此外,您可以通过在命令提示符处发出以下命令来查看安装的最新 运行时和 SDK 版本:

dotnet 最新的运行时版本是第一个列出的。 免责声明: 这不再有效,但可能适用于旧版本。

dotnet --version 最新 SDK 版本 免责声明: 显然,此结果可能会受到任何影响 global.json配置文件。


在 macOS 上,您可以使用以下命令检查 .net 核心版本。

ls /usr/local/share/dotnet/shared/Microsoft.NETCore.App/

在 Ubuntu 或高山:

ls /usr/share/dotnet/shared/Microsoft.NETCore.App/

它会列出安装版本名称的文件夹。

对于 运行没有 SDK 的仅限时间的环境,例如安装了 Windows 托管包的服务器,正确答案是 运行 PowerShell 使用以下命令:

dotnet --info

根据 official documentation:

  • --version 选项 "Prints out the version of the .NET Core SDK in use." 因此在未安装 SDK 时不起作用。而...
  • --info选项"Prints out detailed information about the CLI tooling and the environment, such as the current operating system, commit SHA for the version, and other information."

这是另一篇解释 .NET Core versioning 工作原理的官方文章。 :)

(1) 如果你在Window系统上。

打开命令提示符。

 dotnet --version

(2) 运行 下面的命令 如果你在 Linux 系统上。

dotnet --version

dotnet --info

我主要使用 Windows 开发机器和服务器。

我只是想指出(至少对于 NET.Core 2.0 及更高版本)唯一需要的是在命令提示符下执行 dotnet --info 以获取有关 的信息安装了最新 版本。如果安装了 .NET Core,您会得到一些响应。

在我的开发机(Windows10)上结果如下。 SDK 为 2.1.2,运行时为 2.0.3。

.NET Command Line Tools (2.1.2)

Product Information:
 Version:            2.1.2
 Commit SHA-1 hash:  5695315371

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.15063
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk.1.2\

Microsoft .NET Core Shared Framework Host

  Version  : 2.0.3
  Build    : a9190d4a75f4a982ae4b4fa8d1a24526566c69df

在我的一台服务器上 运行 Windows Server 2016 with Windows Server Hosting pack (no SDK) 结果如下。没有SDK,运行时是2.0.3.

Microsoft .NET Core Shared Framework Host

Version  : 2.0.3
Build    : a9190d4a75f4a982ae4b4fa8d1a24526566c69df

干杯!

使用Powershell:

运行时间:

(dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'shared\Microsoft.NETCore.App')).Name

SDK:

(dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'sdk')).Name

在 windows 上,您只需打开命令提示符并键入:

dotnet --version

如果安装了 .net 核心框架,您将获得当前安装的版本

见截图:

以下命令可用于 .NET Core SDK 2.1 (v2.1.300)

要列出所有已安装的 .NET Core SDK,请使用:dotnet --list-sdks

要列出所有已安装的 .NET Core 运行时,请使用 dotnet --list-runtimes

(在撰写本文时于 Windows 测试,2018 年 6 月 3 日,2018 年 8 月 23 日再次测试)

截至 2018 年 10 月 24 日的更新:更好的选择现在可能是 dotnet --info 在终端或 PowerShell window 中,正如其他答案中已经提到的那样。

C:\Program Files\dotnet\shared\Microsoft.NETCore.App 中查看哪些版本的运行时有目录。

这里很多回答把SDK和Runtime搞混了,两者是不一样的。

在所有其他答案之后,这可能会有用。

在 Visual Studio 中打开您的应用程序。在解决方案资源管理器中,右键单击您的项目。单击属性。单击应用程序。在 "Target Framework" 下单击下拉按钮,所有已安装的框架都在那里。

顺便说一句 - 您现在可以选择所需的框架。

或者你可以只看里面

C:\程序Files\dotnet\sdk

运行 这个命令

dotnet --list-sdks

It's possible that .NET Core is installed but not added to the PATH variable for your operating system or user profile. Running the dotnet commands may not work. As an alternative, you can check that the .NET Core install folders exist.

如果您在安装过程中没有更改它,它会安装到标准文件夹中

  • dotnet 可执行文件C:\program files\dotnet\dotnet.exe

  • .NET SDK C:\program files\dotnet\sdk\{version}\

  • .NET 运行时 C:\program files\dotnet\shared\{runtime-type}\{version}\

有关详细信息,请查看 .NET 文档中的 How to check that .NET Core is already installed 页面

您可以通过终端查看当前安装了哪些版本的.NET Core SDK。打开终端并运行以下命令。

dotnet --list-sdks
dotnet --info

dotnet --version

在您的 CMD终端 上写入上述命令。然后它会显示类似下面的内容

你可以使用 运行> 命令 > dotnet --version

此方法仅适用于 Windows,可能有点矫枉过正。

function Get-InstalledApps {
  [CmdletBinding(SupportsShouldProcess=$false)]
  Param ([Parameter(Mandatory=$false, ValueFromPipeline=$true)] [string]$ComputerName=$env:COMPUTERNAME,
         [Parameter(Mandatory=$false, ValueFromPipeline=$false)] [System.Management.Automation.PSCredential]$Credential)

  Begin { Write-Verbose "Entering $($PSCmdlet.MyInvocation.MyCommand.Name)" }

  Process {
    $HKEY_LOCAL_MACHINE=2147483650
    $Results=@()

    if ($Credential -eq $null) { $Reg=[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$ComputerName) }
    else { $Reg=Get-WmiObject -Namespace "root\default" -List "StdRegProv" -ComputerName $ComputerName -Credential $Credential }

    $RegPath=@("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")
    if ([IntPtr]::Size -ne 4) {
      $RegPath+="SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    }

    for ($i=0; $i -lt $RegPath.Count; $i++) {
      if ($Credential -eq $null) {
        $RegKey=$Reg.OpenSubKey($RegPath[$i])
        $InstallKeys=$RegKey.GetSubKeyNames()
        $RegKey.Close()
      }
      else { $InstallKeys=$Reg.EnumKey($HKEY_LOCAL_MACHINE,$RegPath[$i]) | Select-Object -ExpandProperty sNames }
      $InstallKeys=@($InstallKeys)

      for ($j=0; $j -lt $InstallKeys.Count; $j++) {
        if ($Credential -eq $null) {
          $AppKey=$Reg.OpenSubKey(($RegPath[$i]+"\"+$InstallKeys[$j]))
          $Result=New-Object -Type PSObject -Property @{ComputerName=$ComputerName;
                                                        DisplayName=$AppKey.GetValue("DisplayName");
                                                        Publisher=$AppKey.GetValue("Publisher");
                                                        InstallDate=$AppKey.GetValue("InstallDate");
                                                        DisplayVersion=$AppKey.GetValue("DisplayVersion");
                                                        UninstallString=$AppKey.GetValue("UninstallString")}
          $AppKey.Close()
        }
        else {
          $Result=New-Object -Type PSObject -Property @{ComputerName=$ComputerName;
                                                        DisplayName=$Reg.GetStringValue($HKEY_LOCAL_MACHINE,($RegPath[$i]+"\"+$InstallKeys[$j]),"DisplayName").sValue;
                                                        Publisher=$Reg.GetStringValue($HKEY_LOCAL_MACHINE,($RegPath[$i]+"\"+$InstallKeys[$j]),"Publisher").sValue;
                                                        InstallDate=$Reg.GetStringValue($HKEY_LOCAL_MACHINE,($RegPath[$i]+"\"+$InstallKeys[$j]),"InstallDate").sValue;
                                                        DisplayVersion=$Reg.GetStringValue($HKEY_LOCAL_MACHINE,($RegPath[$i]+"\"+$InstallKeys[$j]),"DisplayVersion").sValue;
                                                        UninstallString=$Reg.GetStringValue($HKEY_LOCAL_MACHINE,($RegPath[$i]+"\"+$InstallKeys[$j]),"UninstallString").sValue;}
        }
        if ($Result.DisplayName -ne $null) { $Results+=$Result }
      }
    }
    if ($Credential -eq $null ) { $Reg.Close() }
    $Results
  }

  End { Write-Verbose "Exiting $($PSCmdlet.MyInvocation.MyCommand.Name)" }
}

$NetSDK=Get-InstalledApps | Where-Object { $_.DisplayName -like "*.NET Core SDK*" } | Sort-Object -Property DisplayVersion -Descending | Select-Object -First 1
$NetHost=Get-InstalledApps | Where-Object { $_.DisplayName -like "*ASP.NET Core*" } | Sort-Object -Property DisplayVersion -Descending | Select-Object -First 1
$NetSDK
$NetHost

在 windows 我检查了注册表项:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET Core\Shared Framework\v5.0.0.12

--在命令上

对于 .Net 框架

wmic product get description | findstr /C:".NET Framework

对于 .Net 核心

dotnet --info

我来这里是为了寻找一种程序化的方法来确定这一点;虽然这个问题有很多好的答案,但 none 其中似乎是程序化的。

我用 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
        }
    }
}