在 Windows 上安装 .NET CoreInstall .NET Core on Windows

在本文中,你将了解如何在 Windows 上安装 .NET Core。In this article, you'll learn how to install .NET Core on Windows. .NET Core 由运行时和 SDK 组成。.NET Core is made up of the runtime and the SDK. 运行时用于运行 .NET Core 应用,应用可能包含也可能不包含它。The runtime is used to run a .NET Core app and may or may not be included with the app. SDK 用于创建 .NET Core 应用和库。The SDK is used to create .NET Core apps and libraries. .NET Core 运行时始终随 SDK 一起安装。The .NET Core runtime is always installed with the SDK.

最新版本的 .NET Core 是 3.1。The latest version of .NET Core is 3.1.

支持的版本Supported releases

下表列出了当前支持的 .NET Core 版本以及支持它们的 Windows 版本。The following table is a list of currently supported .NET Core releases and the versions of Windows they're supported on. 这些版本在 .NET Core 版本达到支持终止日期Windows 版本达到生命周期之前仍受支持。These versions remain supported until either the version of .NET Core reaches end-of-support or the version of Windows reaches end-of-life.

Windows 10 版本终止服务日期按版本分段。Windows 10 versions end-of-service dates are segmented by edition. 下表中仅考虑家庭版、专业版、专业教育版和专业工作站版。 Only Home, Pro, Pro Education, and Pro for Workstations editions are considered in the following table. 查看 Windows 生命周期事实表单,了解具体的详细信息。Check the Windows lifecycle fact sheet for specific details.

  • ✔️ 指示 Windows 或 .NET Core 版本仍受支持。A ✔️ indicates that the version of Windows or .NET Core is still supported.
  • ❌ 指示 Windows 或 .NET Core 版本在该 Windows 版本上不受支持。A ❌ indicates that the version of Windows or .NET Core isn't supported on that Windows release.
  • 当 Windows 版本和 .NET Core 版本都有 ✔️ 时,支持该 OS 和 .NET 组合。When both a version of Windows and a version of .NET Core have ✔️, that OS and .NET combination are supported.
操作系统Operating System .NET Core 2.1.NET Core 2.1 .NET Core 3.1.NET Core 3.1 .NET 5 预览版.NET 5 Preview
✔️ Windows 10 版本 2004✔️ Windows 10, Version 2004 ✔️ 2.1✔️ 2.1 ✔️ 3.1✔️ 3.1 ✔️ 5.0 预览版✔️ 5.0 Preview
✔️ Windows 10 版本 1909✔️ Windows 10, Version 1909 ✔️ 2.1✔️ 2.1 ✔️ 3.1✔️ 3.1 ✔️ 5.0 预览版✔️ 5.0 Preview
✔️ Windows 10 版本 1903✔️ Windows 10, Version 1903 ✔️ 2.1✔️ 2.1 ✔️ 3.1✔️ 3.1 ✔️ 5.0 预览版✔️ 5.0 Preview
✔️ Windows 10 版本 1809✔️ Windows 10, Version 1809 ✔️ 2.1✔️ 2.1 ✔️ 3.1✔️ 3.1 ✔️ 5.0 预览版✔️ 5.0 Preview
❌ Windows 10 版本 1803❌ Windows 10, Version 1803 ✔️ 2.1✔️ 2.1 ❌ 3.1❌ 3.1 ❌ 5.0 预览版❌ 5.0 Preview
❌ Windows 10 版本 1709❌ Windows 10, Version 1709 ❌ 2.1❌ 2.1 ❌ 3.1❌ 3.1 ❌ 5.0 预览版❌ 5.0 Preview
❌ Windows 10 版本 1703❌ Windows 10, Version 1703 ❌ 2.1❌ 2.1 ❌ 3.1❌ 3.1 ❌ 5.0 预览版❌ 5.0 Preview
❌ Windows 10 版本 1607❌ Windows 10, Version 1607 ❌ 2.1❌ 2.1 ❌ 3.1❌ 3.1 ❌ 5.0 预览版❌ 5.0 Preview
❌ Windows 10 版本 1511❌ Windows 10, Version 1511 ❌ 2.1❌ 2.1 ❌ 3.1❌ 3.1 ❌ 5.0 预览版❌ 5.0 Preview
❌ Windows 10 版本 1507❌ Windows 10, Version 1507 ❌ 2.1❌ 2.1 ❌ 3.1❌ 3.1 ❌ 5.0 预览版❌ 5.0 Preview

不支持的版本Unsupported releases

以下 .NET Core 版本 ❌ 不再受支持。The following versions of .NET Core are ❌ no longer supported. 这些版本的下载仍保持发布状态:The downloads for these still remain published:

  • 3.03.0
  • 2.22.2
  • 2.02.0

运行时信息Runtime information

运行时用于运行使用 .NET Core 创建的应用。The runtime is used to run apps created with .NET Core. 应用作者发布应用时,可以在其应用中包含运行时。When an app author publishes an app, they can include the runtime with their app. 如果作者未包含运行时,则由用户安装运行时。If they don't include the runtime, it's up to the user to install the runtime.

可以在 Windows 上安装三个不同的运行时:There are three different runtimes you can install on Windows:

ASP.NET Core 运行时ASP.NET Core runtime
运行 ASP.NET Core 应用。Runs ASP.NET Core apps. 包括 .NET Core 运行时。Includes the .NET Core runtime.

桌面运行时Desktop runtime
运行适用于 Windows 的 .NET Core WPF 和 .NET Core Windows 窗体桌面应用。Runs .NET Core WPF and .NET Core Windows Forms desktop apps for Windows. 包括 .NET Core 运行时。Includes the .NET Core runtime.

.NET Core 运行时.NET Core runtime
此运行时是最简单的运行时,不包括任何其他运行时。This runtime is the simplest runtime and doesn't include any other runtime. 强烈建议同时安装 ASP.NET Core 运行时和桌面运行时,以最大限度地提升与 .NET Core 应用的兼容性。 It's highly recommended that you install both ASP.NET Core runtime and Desktop runtime for the best compatibility with .NET Core apps.

SDK 信息SDK information

SDK 用于生成和发布 .NET Core 应用和库。The SDK is used to build and publish .NET Core apps and libraries. 安装 SDK 会包含三个运行时:ASP.NET Core、桌面和 .NET Core。Installing the SDK includes all three runtimes: ASP.NET Core, Desktop, and .NET Core.

依赖项Dependencies

.NET Core 3.1 支持下列 Windows 版本:The following Windows versions are supported with .NET Core 3.1:

备注

+ 表示最低版本。A + symbol represents the minimum version.

(OS)OS VersionVersion 体系结构Architectures
Windows 客户端Windows Client 7 SP1+、8.17 SP1+, 8.1 x64、x86x64, x86
Windows 10 客户端Windows 10 Client 版本 1609+Version 1609+ x64、x86x64, x86
Windows ServerWindows Server 2012 R2+2012 R2+ x64、x86x64, x86
Nano ServerNano Server 版本 1803+Version 1803+ x64、ARM32x64, ARM32

有关 .NET Core 3.1 支持的操作系统、发行版和生命周期策略的详细信息,请参阅 .NET Core 3.1 支持的 OS 版本For more information about .NET Core 3.1 supported operating systems, distributions, and lifecycle policy, see .NET Core 3.1 Supported OS Versions.

Windows 7 / Vista / 8.1 / Server 2008 R2 / Server 2012 R2Windows 7 / Vista / 8.1 / Server 2008 R2 / Server 2012 R2

如果要在以下 Windows 版本上安装 .NET SDK 或运行时,则需要其他依赖项:Additional dependencies are required if you're installing the .NET SDK or runtime on the following Windows versions:

  • ❌ Windows 7 SP1❌ Windows 7 SP1
  • ❌ Windows Vista SP 2❌ Windows Vista SP 2
  • ✔️ Windows 8.1✔️ Windows 8.1
  • ✔️ Windows Server 2008 R2✔️ Windows Server 2008 R2
  • ✔️ Windows Server 2012 R2✔️ Windows Server 2012 R2

安装以下组件:Install the following:

如果遇到一个以下错误,也需要满足上述要求:The requirements above are also required if you come across one of the following errors:

此程序无法启动,因为计算机上缺少 api-ms-win-crt-runtime-l1-1-0.dll。The program can't start because api-ms-win-crt-runtime-l1-1-0.dll is missing from your computer. 尝试重新安装该程序以解决此问题。Try reinstalling the program to fix this problem.

- 或 -- or -

此程序无法启动,因为计算机上缺少 api-ms-win-cor-timezone-l1-1-0.dll。The program can't start because api-ms-win-cor-timezone-l1-1-0.dll is missing from your computer. 尝试重新安装该程序以解决此问题。Try reinstalling the program to fix this problem.

- 或 -- or -

已找到库 hostfxr.dll,但未能将其从 C:\<path_to_app>\hostfxr.dll 中加载。The library hostfxr.dll was found, but loading it from C:\<path_to_app>\hostfxr.dll failed.

使用 PowerShell 自动化安装Install with PowerShell automation

dotnet-install 脚本用于运行时的 CI 自动化和非管理员安装。The dotnet-install scripts are used for CI automation and non-admin installs of the runtime. 可从 dotnet-install 脚本引用页下载该脚本。You can download the script from the dotnet-install script reference page.

此脚本默认安装最新的长期支持 (LTS) 版本,即 .NET Core 3.1。The script defaults to installing the latest long term support (LTS) version, which is .NET Core 3.1. 可通过指定 Channel 开关以选择特定版本。You can choose a specific release by specifying the Channel switch. 包括 Runtime 开关以安装运行时。Include the Runtime switch to install a runtime. 否则,该脚本安装 SDK。Otherwise, the script installs the SDK.

dotnet-install.ps1 -Channel 3.1 -Runtime aspnetcore

通过省略 -Runtime 开关来安装 SDK。Install the SDK by omitting the -Runtime switch. 在此示例中将 -Channel 开关设置为 Current,这将安装受支持的最新版本。The -Channel switch is set in this example to Current, which installs the latest supported version.

dotnet-install.ps1 -Channel Current

使用 Visual Studio 安装Install with Visual Studio

如果你要使用 Visual Studio 开发 .NET Core 应用,请参阅下表,了解不同目标 .NET Core SDK 版本所需的 Visual Studio 最低版本。If you're using Visual Studio to develop .NET Core apps, the following table describes the minimum required version of Visual Studio based on the target .NET Core SDK version.

.NET Core SDK 版本.NET Core SDK version Visual Studio 版本Visual Studio version
3.13.1 Visual Studio 2019 版本 16.4 或更高版本。Visual Studio 2019 version 16.4 or higher.
3.03.0 Visual Studio 2019 版本 16.3 或更高版本。Visual Studio 2019 version 16.3 or higher.
2.22.2 Visual Studio 2017 版本 15.9 或更高版本。Visual Studio 2017 version 15.9 or higher.
2.12.1 Visual Studio 2017 版本 15.7 或更高版本。Visual Studio 2017 version 15.7 or higher.

如果你已安装 Visual Studio,则可以使用以下步骤检查你的版本。If you already have Visual Studio installed, you can check your version with the following steps.

  1. 打开 Visual Studio。Open Visual Studio.
  2. 选择“帮助” > “Microsoft Visual Studio”。Select Help > About Microsoft Visual Studio.
  3. 从“关于”对话框中读取版本号。Read the version number from the About dialog.

Visual Studio 可安装最新的 .NET Core SDK 和运行时。Visual Studio can install the latest .NET Core SDK and runtime.

选择工作负载Select a workload

安装或修改 Visual Studio 时,根据要生成的应用程序的类型,选择以下一个或多个工作负载:When installing or modifying Visual Studio, select one or more of the following workloads, depending on the kind of application you're building:

  • “其他工具集”部分中的“.NET Core 跨平台开发”工作负荷 。The .NET Core cross-platform development workload in the Other Toolsets section.
  • “Web 和云”部分中的“ASP.NET 和 Web 开发”工作负荷 。The ASP.NET and web development workload in the Web & Cloud section.
  • “Web 和云”部分中的“Azure 开发”工作负载 。The Azure development workload in the Web & Cloud section.
  • “桌面和移动”部分中的“NET 桌面开发”工作负载 。The .NET desktop development workload in the Desktop & Mobile section.

具有 .NET Core 工作负载的 Windows Visual Studio 2019Windows Visual Studio 2019 with .NET Core workload

随 Visual Studio Code 一起安装Install alongside Visual Studio Code

Visual Studio Code 是一个功能强大的轻量级源代码编辑器,可在桌面上运行。Visual Studio Code is a powerful and lightweight source code editor that runs on your desktop. Visual Studio Code 适用于 Windows、macOS 和 Linux。Visual Studio Code is available for Windows, macOS, and Linux.

虽然 Visual Studio Code 不像 Visual Studio 一样附带自动的 .NET Core 安装程序,但添加 .NET Core 支持非常简单。While Visual Studio Code doesn't come with an automated .NET Core installer like Visual Studio does, adding .NET Core support is simple.

  1. 下载并安装 Visual Studio CodeDownload and install Visual Studio Code.
  2. 下载并安装 .NET Core SDKDownload and install the .NET Core SDK.
  3. 从 Visual Studio Code 市场安装 C# 扩展Install the C# extension from the Visual Studio Code marketplace.

下载并手动安装Download and manually install

除了使用适用于 .NET Core 的 Windows 安装程序,还可以下载并手动安装 SDK 或运行时。As an alternative to the Windows installers for .NET Core, you can download and manually install the SDK or runtime. 手动安装通常作为持续集成测试的一部分执行。Manual install is usually performed as part of continuous integration testing. 对于开发人员或用户,一般使用安装程序会更好。For a developer or user, it's generally better to use an installer.

在下载 .NET Core SDK 和 .NET Core 运行时后,可以手动安装它们。Both .NET Core SDK and .NET Core Runtime can be manually installed after they've been downloaded. 如果安装 .NET Core SDK,则无需安装相应的运行时。If you install .NET Core SDK, you don't need to install the corresponding runtime. 首先,从以下站点之一下载 SDK 或运行时的二进制版本:First, download a binary release for either the SDK or the runtime from one of the following sites:

创建要将 .NET 提取到的目录,例如 %USERPROFILE%\dotnetCreate a directory to extract .NET to, for example %USERPROFILE%\dotnet. 然后,将下载的 zip 文件提取到该目录中。Then, extract the downloaded zip file into that directory.

默认情况下,.NET Core CLI 命令和应用不会使用通过这种方式安装的 .NET Core,且你必须显式选择以使用它。By default, .NET Core CLI commands and apps won't use .NET Core installed in this way and you must explicitly choose to use it. 为此,请更改用于启动应用程序的环境变量:To do so, change the environment variables with which an application is started:

set DOTNET_ROOT=%USERPROFILE%\dotnet
set PATH=%USERPROFILE%\dotnet;%PATH%
set DOTNET_MULTILEVEL_LOOKUP=0

使用此方法可以将多个版本安装到不同的位置,然后通过使用指向安装位置的环境变量运行应用程序来明确选择应用程序应使用哪个安装位置。This approach lets you install multiple versions into separate locations, then explicitly choose which install location an application should use by running the application with environment variables pointing at that location.

DOTNET_MULTILEVEL_LOOKUP 设置为 0 时,.NET Core 将忽略任何全局安装的 .NET Core 版本。When DOTNET_MULTILEVEL_LOOKUP is set to 0, .NET Core ignores any globally installed .NET Core version. 删除环境设置,让 .NET Core 在选择用于运行应用程序的最佳框架时考虑默认的全局安装位置。Remove that environment setting to let .NET Core consider the default global install location when selecting the best framework for running the application. 默认值通常为 C:\Program Files\dotnet,这是安装 .NET Core 的安装程序所在的位置。The default is typically C:\Program Files\dotnet, which is where the installers install .NET Core.

DockerDocker

容器提供了一种将应用程序与主机系统的其余部分隔离的轻量级方法。Containers provide a lightweight way to isolate your application from the rest of the host system. 同一计算机上的容器只共享内核,并使用为应用程序提供的资源。Containers on the same machine share just the kernel and use resources given to your application.

.NET Core 可在 Docker 容器中运行。.NET Core can run in a Docker container. 官方 .NET Core Docker 映像发布到 Microsoft 容器注册表 (MCR),用户可以在 Microsoft.NET Core Docker 中心存储库中找到这些映像。Official .NET Core Docker images are published to the Microsoft Container Registry (MCR) and are discoverable at the Microsoft .NET Core Docker Hub repository. 每个存储库包含 .NET(SDK 或运行时)和可以使用的操作系统的不同组合的映像。Each repository contains images for different combinations of the .NET (SDK or Runtime) and OS that you can use.

Microsoft 提供适合特定场景的映像。Microsoft provides images that are tailored for specific scenarios. 例如,ASP.NET Core 存储库提供针对在生产环境中运行 ASP.NET Core 应用生成的映像。For example, the ASP.NET Core repository provides images that are built for running ASP.NET Core apps in production.

有关在 Docker 容器中使用 .NET Core 的详细信息,请参阅 .NET 和 Docker 简介示例For more information about using .NET Core in a Docker container, see Introduction to .NET and Docker and Samples.

后续步骤Next steps