使用 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> --version <VERSION>

例如,要添加 Newtonsoft.Json 包的 12.0.1 版,请使用以下命令:For example, to add version 12.0.1 of the Newtonsoft.Json package, use this command:

dotnet add package Newtonsoft.Json --version 12.0.1

NuGet 包引用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

除非指定包版本,否则 NuGet 会在使用 dotnet add package 命令时安装最新版本的包(-v 开关)。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