在 Windows 上安装 .NETInstall .NET on Windows

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

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

支持的版本Supported releases

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

提示

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

操作系统Operating System .NET Core 2.1.NET Core 2.1 .NET Core 3.1.NET Core 3.1 .NET 5.NET 5
Windows 10/Windows Server 版本 20H2Windows 10 / Windows Server, Version 20H2 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10/Windows Server 版本 2004Windows 10 / Windows Server, Version 2004 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10/Windows Server 版本 1909Windows 10 / Windows Server, Version 1909 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10/Windows Server 版本 1903Windows 10 / Windows Server, Version 1903 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10 版本 1809Windows 10, Version 1809 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10 版本 1803Windows 10, Version 1803 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10 版本 1709Windows 10, Version 1709 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10 版本 1607Windows 10, Version 1607 ✔️✔️ ✔️✔️ ✔️✔️
Windows 8.1Windows 8.1 ✔️✔️ ✔️✔️ ✔️✔️
Windows 7 SP1 ESUWindows 7 SP1 ESU ✔️✔️ ✔️✔️ ✔️✔️
Windows Server 2019Windows Server 2019
Windows Server 2016Windows Server 2016
Windows Server 2012 R2Windows Server 2012 R2
✔️✔️ ✔️✔️ ✔️✔️
Windows Server Core 2012 R2Windows Server Core 2012 R2 ✔️✔️ ✔️✔️ ✔️✔️
Nano Server 版本 1809+Nano Server, Version 1809+ ✔️✔️ ✔️✔️ ✔️✔️
Nano Server 版本 1803Nano Server, Version 1803 ✔️✔️ ✔️✔️

不支持的版本Unsupported releases

以下 .NET 版本 ❌ 不再受到支持:The following versions of .NET are ❌ no longer supported:

  • 3.03.0
  • 2.22.2
  • 2.02.0

运行时信息Runtime information

运行时用于运行使用 .NET 创建的应用。The runtime is used to run apps created with .NET. 应用作者发布应用时,可以在其应用中包含运行时。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 运行时。Includes the .NET runtime.

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

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

SDK 信息SDK information

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

依赖项Dependencies

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

备注

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

(OS)OS VersionVersion 体系结构Architectures
Windows 10 客户端Windows 10 Client 版本 1607+Version 1607+ x64、x86、ARM64x64, x86, ARM64
Windows 客户端Windows Client 7 SP1+、8.17 SP1+, 8.1 x64、x86x64, x86
Windows ServerWindows Server 2012 R2+2012 R2+ x64、x86x64, x86
Windows Server 核心Windows Server Core 2012 R2+2012 R2+ x64、x86x64, x86
Nano ServerNano Server 版本 1809+Version 1809+ X64x64

有关 .NET 5.0 支持的操作系统、发行版和生命周期策略的详细信息,请参阅 .NET 5.0 支持的 OS 版本For more information about .NET 5.0 supported operating systems, distributions, and lifecycle policy, see .NET 5.0 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 或运行时,则需要其他依赖项:More dependencies are required if you're installing the .NET SDK or runtime on the following Windows versions:

操作系统Operating System 先决条件Prerequisites
Windows 7 SP1 ESUWindows 7 SP1 ESU - Microsoft Visual C++ 2015-2019 Redistributable 64 位 / 32 位- Microsoft Visual C++ 2015-2019 Redistributable 64-bit / 32-bit
- KB3063858 64 位 / 32 位- KB3063858 64-bit / 32-bit
- Microsoft 根证书颁发机构 2011(仅限 .NET Core 2.1 脱机安装程序)- Microsoft Root Certificate Authority 2011 (.NET Core 2.1 offline installer only)
Windows Vista SP 2Windows Vista SP 2 Microsoft Visual C++ 2015-2019 Redistributable 64 位 / 32 位Microsoft Visual C++ 2015-2019 Redistributable 64-bit / 32-bit
Windows 8.1Windows 8.1 Microsoft Visual C++ 2015-2019 Redistributable 64 位 / 32 位Microsoft Visual C++ 2015-2019 Redistributable 64-bit / 32-bit
Windows Server 2008 R2Windows Server 2008 R2 Microsoft Visual C++ 2015-2019 Redistributable 64 位 / 32 位Microsoft Visual C++ 2015-2019 Redistributable 64-bit / 32-bit
Windows Server 2012 R2Windows Server 2012 R2 Microsoft Visual C++ 2015-2019 Redistributable 64 位 / 32 位Microsoft Visual C++ 2015-2019 Redistributable 64-bit / 32-bit

如果收到与以下 dll 之一相关的错误,也需要满足上述要求:The previous requirements are also required if you receive an error related to either of the following dlls:

  • api-ms-win-crt-runtime-l1-1-0.dllapi-ms-win-crt-runtime-l1-1-0.dll
  • api-ms-win-cor-timezone-l1-1-0.dllapi-ms-win-cor-timezone-l1-1-0.dll
  • hostfxr.dllhostfxr.dll

使用 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 5.0 -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 应用,请参阅下表,了解不同目标 .NET SDK 版本所需的 Visual Studio 最低版本。If you're using Visual Studio to develop .NET apps, the following table describes the minimum required version of Visual Studio based on the target .NET SDK version.

.NET SDK 版本.NET SDK version Visual Studio 版本Visual Studio version
5.05.0 Visual Studio 2019 版本 16.8 或更高版本。Visual Studio 2019 version 16.8 or higher.
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 SDK 和运行时。Visual Studio can install the latest .NET 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.

Windows InstallerWindows Installer

适用于 .NET 的下载页面提供了 Windows Installer 可执行文件。The download page for .NET provides Windows Installer executables.

使用 Windows 安装程序安装 .NET 时,可以通过设置 DOTNETHOME_X64DOTNETHOME_X86 参数来自定义安装路径:When you use the Windows installers to install .NET, you can customize the installation path by setting the DOTNETHOME_X64 and DOTNETHOME_X86 parameters:

dotnet-sdk-3.1.301-win-x64.exe DOTNETHOME_X64="F:\dotnet\x64" DOTNETHOME_X86="F:\dotnet\x86"

如果要以无提示方式安装 .NET(例如在生产环境中)或要支持持续集成,请使用以下开关:If you want to install .NET silently, such as in a production environment or to support continuous integration, use the following switches:

  • /install
    安装 .NET。Installs .NET.

  • /quiet
    禁止显示任何 UI 和提示。Prevents any UI and prompts from displaying.

  • norestart
    禁止任何重启尝试。Suppresses any attempts to restart.

dotnet-sdk-3.1.301-win-x64.exe /install /quiet /norestart

有关详细信息,请参阅标准安装程序命令行选项For more information, see Standard Installer Command-Line Options.

提示

安装程序返回退出代码 0 以表示成功,返回退出代码 3010 以表示需要重启。The installer returns an exit code of 0 for success and an exit code of 3010 to indicate that a restart is required. 任何其他值通常都是错误代码。Any other value is generally an error code.

下载并手动安装Download and manually install

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

在下载 .NET SDK 和 .NET 运行时后,可以手动安装它们。Both .NET SDK and .NET Runtime can be manually installed after they've been downloaded. 如果安装 .NET SDK,则无需安装相应的运行时。If you install .NET 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 CLI 命令和应用不会使用通过这种方式安装的 .NET,并且你必须显式选择才能使用它。By default, .NET CLI commands and apps won't use .NET 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 将忽略任何全局安装的 .NET 版本。When DOTNET_MULTILEVEL_LOOKUP is set to 0, .NET ignores any globally installed .NET version. 删除环境设置,让 .NET 在选择用于运行应用程序的最佳框架时考虑默认的全局安装位置。Remove that environment setting to let .NET consider the default global install location when selecting the best framework for running the application. 默认值通常为 C:\Program Files\dotnet,这是安装 .NET 的安装程序所在的位置。The default is typically C:\Program Files\dotnet, which is where the installers install .NET.

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 可在 Docker 容器中运行。.NET can run in a Docker container. 官方 .NET Docker 映像发布到 Microsoft 容器注册表 (MCR),用户可在 Microsoft.NET Docker Hub 存储库中找到这些映像。Official .NET Docker images are published to the Microsoft Container Registry (MCR) and are discoverable at the Microsoft .NET 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 的详细信息,请参阅 .NET 和 Docker 简介示例For more information about using .NET in a Docker container, see Introduction to .NET and Docker and Samples.

后续步骤Next steps