使用 nuget.exe CLI 管理包Manage packages using the nuget.exe CLI

通过 CLI 工具可轻松更新和还原项目和解决方案中的 NuGet 包。The CLI tool allows you to easily update and restore NuGet packages in projects and solutions. 该工具提供 Windows 上的所有 NuGet 功能以及 Mac 和 Linux 上在 Mono 下运行时的大多数功能。This tool provides all NuGet capabilities on Windows, and also provides most features on Mac and Linux when running under Mono.

nuget.exe CLI 适用于 .NET Framework 项目和非 SDK 样式项目(例如,面向 .NET Standard 库的非 SDK 样式项目)。The nuget.exe CLI is for your .NET Framework project and non-SDK-style projects (for example, a non-SDK style project that targets .NET Standard libraries). 如果你使用的是已迁移到 PackageReference 的非 SDK 样式项目,请改用 dotnet CLI。If you are using a non-SDK-style project that has been migrated to PackageReference, use the dotnet CLI instead. nuget.exe CLI 需要 packages.config 文件来进行包引用。The nuget.exe CLI requires a packages.config file for package references.

备注

在大多数情况下,建议将使用 packages.config 的非 SDK 样式项目迁移至 PackageReference,然后可以使用 dotnet CLI 而不是 nuget.exe CLI。In most scenarios, we recommend migrating non-SDK-style projects that use packages.config to PackageReference, and then you can use the dotnet CLI instead of the nuget.exe CLI. 目前,C++ 和 ASP.NET 项目无法进行迁移。Migration is not currently available for C++ and ASP.NET projects.

本文介绍了一些最常见的 nuget.exe CLI 命令的基本用法。This article shows you basic usage for a few of the most common nuget.exe 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. 有关命令和可能使用的参数的完整列表,请参阅 nuget.exe CLI 参考For a complete list of commands and the arguments you may use, see the nuget.exe CLI reference.

必备条件Prerequisites

  • 要安装 nuget.exe CLI,从 nuget.org 下载它,将 .exe 文件保存到合适的文件夹,然后将该文件夹添加到 PATH 环境变量中。Install the nuget.exe CLI by downloading it from nuget.org, saving that .exe file to a suitable folder, and adding that folder to your PATH environment variable.

安装包Install a package

install 命令使用指定的包源将包下载并安装到项目中,默认为当前文件夹。The install command downloads and installs a package into a project, defaulting to the current folder, using specified package sources. 将新包安装到项目根目录的“包”文件夹中 。Install new packages into the packages folder in your project root directory.

重要

install 命令不会修改项目文件或 packages.config ;在这种方式下,它类似于 restore,因为它只向磁盘添加包,而不更改项目的依赖项。The installcommand does not modify a project file or packages.config ; in this way it's similar to restore in that it only adds packages to disk but does not change a project's dependencies. 要添加依赖项,请通过 Visual Studio 中的包管理器 UI 或控制台添加包,或修改 packages.config ,然后运行 installrestoreTo add a dependency, either add a package through the Package Manager UI or Console in Visual Studio, or modify packages.config and then run either install or restore.

  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 to the packages folder.

    nuget install <packageID> -OutputDirectory packages
    

    要将 Newtonsoft.json 包安装到“包”文件夹,请使用以下命令 :To install the Newtonsoft.json package to the packages folder, use the following command:

    nuget install Newtonsoft.Json -OutputDirectory packages
    

或者可以使用以下命令将现有 packages.config 文件的 NuGet 包安装到“包” 文件夹。Alternatively, you can use the following command to install a NuGet package using an existing packages.config file to the packages folder. 该操作不会将包添加到项目依赖项中,而是在本地安装它。This does not add the package to your project dependencies, but installs it locally.

nuget install packages.config -OutputDirectory packages

安装特定版本的包Install a specific version of a package

如果在使用 install 命令时未指定版本,NuGet 将安装最新版本的包。If the version is not specified when you use the install command, NuGet installs the latest version of the package. 还可以安装特定版本的 Nuget 包:You can also install a specific version of a Nuget package:

nuget install <packageID | configFilePath> -Version <version>

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

nuget install Newtonsoft.Json -Version 12.0.1

有关 install 的限制和行为的详细信息,请参阅安装包For more information on the limitations and behavior of install, see Install a package.

删除包Remove a package

要删除一个或多个包,请从“包”文件夹中删除要删除的包 。To delete one or more packages, delete the packages you want to remove from the packages folder.

如果要重新安装包,请使用 restoreinstall 命令。If you want to reinstall packages, use the restore or install command.

列出包List packages

可以使用 list 命令显示给定源的包列表。You can display a list of packages from a given source using the list command. 使用 -Source 选项限制搜索。Use the -Source option to restrict the search.

nuget list -Source <source>

例如,列出“包”文件夹中的包 。For example, list packages in the packages folder.

nuget list -Source C:\Users\username\source\repos\MyProject\packages

如果使用搜索词,则搜索包括包名称、标记和包描述。If you use a search term, the search includes names of packages, tags, and package descriptions.

nuget list <search term>

更新单个包Update an individual package

除非指定包版本,否则 NuGet 会在使用 install 命令时安装最新版本的包。NuGet installs the latest version of the package when you use the install command unless you specify the package version.

更新所有包Update all packages

使用 update 命令更新所有包。Use the update command to update all packages. 将项目中的所有包(使用 packages.config)更新为其最新可用版本。Updates all packages in a project (using packages.config) to their latest available versions. 建议在运行 update 之前运行 restoreIt is recommended to run restore before running update.

nuget update

还原包Restore packages

使用 restore 命令可下载并安装“包”文件夹中缺少的所有包。 Use the restore command, which downloads and installs any packages missing from the packages folder.

对于迁移到 PackageReference 的项目,请使用 msbuild -t:restore 还原程序包。For projects migrated to PackageReference, use msbuild -t:restore to restore packages instead.

restore 仅将包添加到磁盘,但不会更改项目的依赖项。restore only adds packages to disk but does not change a project's dependencies. 要还原项目依赖项,请修改 packages.config,然后使用 restore 命令。To restore project dependencies, modify packages.config, then use the restore command.

与其他 nuget.exe CLI 命令一样,先打开命令行并切换到包含项目文件的目录。As with the other nuget.exe CLI commands, first open a command line and switch to the directory that contains your project file.

要使用 restore 还原包:To restore a package using restore:

nuget restore MySolution.sln

获取 CLI 版本Get the CLI version

使用此命令:Use this command:

nuget help

帮助输出中的第一行显示版本。The first line in the help output shows the version. 若要避免向上滚动,请改用 nuget help | moreTo avoid scrolling up, use nuget help | more instead.