使用 dotnet CLI 安裝和管理套件Install and manage packages using the dotnet CLI

CLI 工具可讓您輕鬆地在專案和解決方案中安裝、解除安裝及更新 NuGet 套件。The CLI tool allows you to easily install, uninstall, and update NuGet packages in projects and solutions. 可在 Windows、Mac OS X 及 Linux 上執行。It runs on Windows, Mac OS X, and Linux.

Dotnet CLI 適用於 .NET Core 和 .NET Standard 專案 (SDK 樣式專案類型),以及其他任何 SDK 樣式專案 (例如以 .NET Framework 為目標的 SDK 樣式專案)。The dotnet CLI is for use in your .NET Core and .NET Standard project (SDK-style project types), and for any other SDK-style projects (for example, an SDK-style project that targets .NET Framework). 如需詳細資訊,請參閱 SDK 屬性For more information, see SDK attribute.

本文顯示一些最常用 dotnet CLI 命令的基本使用方式。This article shows you basic usage for a few of the most common dotnet CLI commands. 針對這些命令的大部分,CLI 工具會在目前的目錄中尋找專案檔,除非在命令中指定專案檔 (專案檔為選用參數)。For most of these commands, the CLI tool looks for a project file in the current directory, unless a project file is specified in the command (the project file is an optional switch). 如需您可以使用的命令和引數完整清單,請參閱 .NET Core 命令列介面 (CLI) 工具For a complete list of commands and the arguments you may use, see the .NET Core command-line interface (CLI) tools.

必要條件:Prerequisites

  • .NET Core SDK,它會提供 dotnet 命令列工具。The .NET Core SDK, which provides the dotnet command-line tool. 從 Visual Studio 2017 開始,dotnet CLI 會自動與任何 .NET Core 相關工作負載一起安裝。Starting in Visual Studio 2017, the dotnet CLI is automatically installed with any .NET Core related workloads.

安裝套件Install a package

dotnet add package 會將套件參考新增到專案檔中,然後執行 dotnet restore 安裝套件。dotnet add package adds a package reference to the project file, then runs dotnet restore to install the package.

  1. 開啟命令列並切換至包含您專案檔的目錄。Open a command line and switch to the directory that contains your project file.

  2. 使用下列命令來安裝 NuGet 套件:Use the following command to install a Nuget package:

    dotnet add package <PACKAGE_NAME>
    

    例如,若要安裝 Newtonsoft.Json 套件,請使用下列命令For example, to install the Newtonsoft.Json package, use the following command

    dotnet add package Newtonsoft.Json
    
  3. 在命令完成之後,請查看專案檔以確定已安裝套件。After the command completes, look at the project file to make sure the package was installed.

    您可以開啟 .csproj 檔案來查看已新增的參考:You can open the .csproj file to see the added reference:

    <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
    </ItemGroup>
    

安裝特定版本的套件Install a specific version of a package

如果未指定版本,則 NuGet 會安裝最新版的套件。If the version is not specified, NuGet installs the latest version of the package. 您也可以使用 dotnet add package 命令來安裝特定版本的 NuGet 套件:You can also use the dotnet add package command to install a specific version of a Nuget package:

dotnet add package <PACKAGE_NAME> -v <VERSION>

例如,若要新增 12.0.1 版的 Newtonsoft.Json 套件,請使用此命令:For example, to add version 12.0.1 of the Newtonsoft.Json package, use this command:

dotnet add package Newtonsoft.Json -v 12.0.1

列出套件參考List package references

您可以使用 dotnet list package 命令列出專案的套件參考。You can list the package references for your project using the dotnet list package command.

dotnet list package

移除套件Remove a package

使用 dotnet remove package 命令來移除專案檔中的套件參考。Use the dotnet remove package command to remove a package reference from the project file.

dotnet remove package <PACKAGE_NAME>

例如,若要移除 Newtonsoft.Json 套件,請使用下列命令For example, to remove the Newtonsoft.Json package, use the following command

dotnet remove package Newtonsoft.Json

更新套件Update a package

除非您指定套件版本 (-v 參數),否則當您使用 dotnet add package 命令時 NuGet 會安裝最新版的套件。NuGet installs the latest version of the package when you use the dotnet add package command unless you specify the package version (-v switch).

還原套件Restore packages

使用 dotnet restore 命令來還原專案檔 (請參閱 PackageReference) 中所列的套件。Use the dotnet restore command, which restores packages listed in the project file (see PackageReference). 使用 .NET Core 2.0 和更新版本時,會使用 dotnet builddotnet run 自動完成還原。With .NET Core 2.0 and later, restore is done automatically with dotnet build and dotnet run. 截至 NuGet 4.0,這會執行與 nuget restore 相同的程式碼。As of NuGet 4.0, this runs the same code as nuget restore.

如同其他 dotnet CLI 命令,請先開啟命令列並切換至包含您專案檔的目錄。As with the other dotnet CLI commands, first open a command line and switch to the directory that contains your project file.

使用 dotnet restore 還原套件:To restore a package using dotnet restore:

dotnet restore