在 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 1809 版Windows 10, Version 1809 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10 1803 版Windows 10, Version 1803 ✔️✔️ ✔️✔️ ✔️✔️
Windows 10 1709 版Windows 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、Version 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 核心執行時間ASP.NET Core runtime
    執行 ASP.NET Core 應用程式。Runs ASP.NET Core apps. 包含 .NET 執行時間。Includes the .NET runtime.

  • 桌面執行時間Desktop runtime
    執行適用于 Windows 的 .NET WPF 和 Windows Forms 傳統型應用程式。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、Desktop 和 .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.

OSOS 版本Version 架構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 伺服器核心Windows Server Core 2012 R2 +2012 R2+ x64、x86x64, x86
Nano 伺服器Nano Server 版本 1809 +Version 1809+ x64x64

如需 .NET 5.0 支援的作業系統、發行版本和生命週期原則的詳細資訊,請參閱 .net 5.0 支援的作業系統版本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 PrerequisitesPrerequisites
Windows 7 SP1 ESUWindows 7 SP1 ESU -Microsoft Visual c + + 2015-2019 可轉散發套件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 可轉散發套件64位 / 32Microsoft Visual C++ 2015-2019 Redistributable 64-bit / 32-bit
Windows 8.1Windows 8.1 Microsoft Visual c + + 2015-2019 可轉散發套件64位 / 32Microsoft Visual C++ 2015-2019 Redistributable 64-bit / 32-bit
Windows Server 2008 R2Windows Server 2008 R2 Microsoft Visual c + + 2015-2019 可轉散發套件64位 / 32Microsoft Visual C++ 2015-2019 Redistributable 64-bit / 32-bit
Windows Server 2012 R2Windows Server 2012 R2 Microsoft Visual c + + 2015-2019 可轉散發套件64位 / 32Microsoft 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 安裝腳本適用于執行時間的 CI 自動化和非系統管理員安裝。The dotnet-install scripts are used for CI automation and non-admin installs of the runtime. 您可以從 [dotnet-安裝腳本參考] 頁面下載腳本。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. 您可以藉由指定參數來選擇特定版本 ChannelYou 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

藉由省略參數來安裝 SDK -RuntimeInstall 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 & Cloud 區段中的 ASP.NET 和 網頁程式開發 工作負載。The ASP.NET and web development workload in the Web & Cloud section.
  • Web & Cloud 區段中的 Azure 開發 工作負載。The Azure development workload in the Web & Cloud section.
  • Desktop 中的 .net 桌面開發 工作負載 & Mobile 區段。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 Marketplace 安裝 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_X64 DOTNETHOME_X86When 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

如需詳細資訊,請參閱 標準安裝程式 Command-Line 選項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

您也可以下載並手動安裝 SDK 或執行時間,以替代適用于 .NET 的 Windows 安裝程式。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 Container Registry (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