我知道对于旧版本的 .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 机器上看不到相同的东西。
很好的问题,答案并不简单。没有“显示所有 .net 核心版本”命令,但有希望。
编辑:
我不确定它是何时添加的,但 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 或 Alpine 上:
ls /usr/share/dotnet/shared/Microsoft.NETCore.App/
它将列出具有已安装版本名称的文件夹。
使用 Powershell:
运行时:
(dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'shared\Microsoft.NETCore.App')).Name
开发工具包:
(dir (Get-Command dotnet).Path.Replace('dotnet.exe', 'sdk')).Name
[System.Environment]::OSVersion
获取它。我使用 Windows 10 版本 10.0.15063.0 测试了上述命令。它工作正常。
dotnet --info
。
对于没有 SDK 的仅运行时环境(例如安装了 Windows 主机包的服务器),正确答案是使用以下命令运行 PowerShell:
dotnet --info
--version 选项“打印出正在使用的 .NET Core SDK 的版本。”因此,如果未安装 SDK,则不起作用。然而...
--info 选项“打印出有关 CLI 工具和环境的详细信息,例如当前操作系统、版本的提交 SHA 以及其他信息。”
这是另一篇解释 .NET Core versioning 工作原理的官方文章。 :)
--info
在我的服务器上不工作,而 --version
工作。信息选项给了我:Did you mean to run dotnet SDK commands? Please install dotnet SDK from: http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
您可以检查 dotnet.exe 是否可用:
where dotnet
然后您可以检查版本:
dotnet --version
更新:现在有一种更好的方法可以做到这一点,这在许多其他答案中得到了很好的解释:
dotnet --info
ssh
发送到仅安装了运行时的服务器以确认它不起作用。 See this answer instead。
确定 Windows 上是否安装了 .NET Core
的虚拟方法之一是:
按 Windows + R
键入命令
在命令提示符下,键入 dotnet --version
https://i.stack.imgur.com/Sixx7.png
如果安装了 .NET Core
,我们应该不会在上述步骤中收到任何错误。
(1) 如果您在 Window 系统上。
打开命令提示符。
dotnet --version
(2) 如果您使用的是 Linux 系统,请运行以下命令。
dotnet --version
dotnet --info
type dotnet --version
- 在 Windows 10 上不起作用。但 dotnet --version
有效。你确定你的答案还是那个 type
是一个错字?这两个 linux 命令都适用于我的 Win 10。
type
是对读者的指示,而不是命令的一部分。我已经编辑了帖子以匹配。
我主要使用 Windows 开发机器和服务器。
我只是想指出(至少对于 NET.Core 2.0 及更高版本)唯一需要的是在命令提示符下执行 dotnet --info
以获取有关已安装 最新 版本的信息。如果安装了 .NET Core,您会得到一些响应。
在我的开发机器(Windows 10)上,结果如下。 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\2.1.2\
Microsoft .NET Core Shared Framework Host
Version : 2.0.3
Build : a9190d4a75f4a982ae4b4fa8d1a24526566c69df
在我的一台运行 Windows Server 2016 和 Windows Server Hosting Pack(无 SDK)的服务器上,结果如下。没有 SDK,运行时是 2.0.3。
Microsoft .NET Core Shared Framework Host
Version : 2.0.3
Build : a9190d4a75f4a982ae4b4fa8d1a24526566c69df
干杯!
.NET Core SDK 2.1 (v2.1.300) 提供以下命令:
要列出所有已安装的 .NET Core SDK,请使用:dotnet --list-sdks
要列出所有已安装的 .NET Core 运行时,请使用 dotnet --list-runtimes
(截至 2018 年 6 月 3 日在 Windows 上测试,2018 年 8 月 23 日再次测试)
截至 2018 年 10 月 24 日的更新:如其他答案中已经提到的,现在更好的选项可能是终端或 PowerShell 窗口中的 dotnet --info
。
在 Windows 上,您只需要打开命令提示符并键入:
dotnet --version
如果安装了 .net 核心框架,您将获得当前安装的版本
看截图:
https://i.stack.imgur.com/iZ7n1.png
您可以通过终端查看当前安装了哪些版本的 .NET Core SDK。打开终端并运行以下命令。
dotnet --list-sdks
--在CMD上
对于 .Net 框架
wmic product get description | findstr /C:".NET Framework
对于 .Net 核心
dotnet --info
运行此命令
dotnet --list-sdks
https://i.stack.imgur.com/JPyj4.png
dotnet --list-runtimes
用于已安装的运行时
dotnet --info
或者
dotnet --version
https://i.stack.imgur.com/I8R1a.png
或者
https://i.stack.imgur.com/GWTGG.png
此方法仅适用于 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
或者你可以看看里面
C:\Program Files\dotnet\sdk
.NET Core 可能已安装但未添加到您的操作系统或用户配置文件的 PATH 变量中。运行 dotnet 命令可能不起作用。作为替代方案,您可以检查 .NET Core 安装文件夹是否存在。
如果您在灌输过程中没有更改它,它会安装到标准文件夹中
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 页
在 Windows 上,我检查了注册表项:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET Core\Shared Framework\v5.0\5.0.12
HKEY_LOCAL_MACHINE\SOFTWARE\dotnet\Setup\InstalledVersions\x64\sharedhost\Version\6.0.4
我来这里是为了寻找一种程序化的方式来确定这一点;虽然这个问题有很多很好的答案,但它们似乎都不是程序化的。
我在 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
}
}
}
查看 C:\Program Files\dotnet\shared\Microsoft.NETCore.App
以查看哪些运行时版本在其中有目录。 Source.
这里的很多答案都将 SDK 与 Runtime 混淆了,它们是不同的。
在所有其他答案之后,这可能证明是有用的。
在 Visual Studio 中打开您的应用程序。在解决方案资源管理器中,右键单击您的项目。单击属性。单击应用程序。在“目标框架”下,单击下拉按钮,所有已安装的框架都在那里。
顺便说一句 - 你现在可以选择你想要的框架。
https://i.stack.imgur.com/UyErG.png
不定期副业成功案例分享
ls /usr/local/share/dotnet/shared/Microsoft.NETCore.App/
dotnet --version
列出了正在使用的 SDK,默认情况下是最新的,但并非总是如此。如果您的文件夹结构中有 global.json 文件,它将显示 global.json 中设置的任何版本,而不是最新版本。