dotnet add packagedotnet add package

本文适用于: ✔️ .NET Core 2.x SDK 及更高版本This article applies to: ✔️ .NET Core 2.x SDK and later versions


dotnet add package - 向项目文件添加包引用。dotnet add package - Adds a package reference to a project file.


dotnet add [<PROJECT>] package <PACKAGE_NAME>
    [-f|--framework <FRAMEWORK>] [--interactive]
    [-n|--no-restore] [--package-directory <PACKAGE_DIRECTORY>]
    [--prerelease] [-s|--source <SOURCE>] [-v|--version <VERSION>]

dotnet add package -h|--help


使用 dotnet add package 命令可方便地向项目文件添加包引用。The dotnet add package command provides a convenient option to add a package reference to a project file. 运行该命令后,还有一个兼容性检查,确保包与项目中的框架兼容。After running the command, there's a compatibility check to ensure the package is compatible with the frameworks in the project. 如果通过了该检查,则将 <PackageReference> 元素添加到项目文件并运行 dotnet 还原If the check passes, a <PackageReference> element is added to the project file and dotnet restore is run.

例如,将 Newtonsoft.Json 添加到 ToDo.csproj 后的输出如以下示例所示:For example, adding Newtonsoft.Json to ToDo.csproj produces output similar to the following example:

Writing C:\Users\me\AppData\Local\Temp\tmp95A8.tmp
info : Adding PackageReference for package 'Newtonsoft.Json' into project 'C:\projects\ToDo\ToDo.csproj'.
log  : Restoring packages for C:\Temp\projects\consoleproj\consoleproj.csproj...
info :   GET
info :   OK 79ms
info :   GET
info :   OK 232ms
log  : Installing Newtonsoft.Json 12.0.1.
info : Package 'Newtonsoft.Json' is compatible with all the specified frameworks in project 'C:\projects\ToDo\ToDo.csproj'.
info : PackageReference for package 'Newtonsoft.Json' version '12.0.1' added to file 'C:\projects\ToDo\ToDo.csproj'.

ToDo.csproj 文件现包含用于引用的包的 <PackageReference> 元素。The ToDo.csproj file now contains a <PackageReference> element for the referenced package.

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

隐式还原Implicit restore

无需运行 dotnet restore,因为它由所有需要还原的命令隐式运行,如 dotnet newdotnet builddotnet rundotnet testdotnet publishdotnet packYou don't have to run dotnet restore because it's run implicitly by all commands that require a restore to occur, such as dotnet new, dotnet build, dotnet run, dotnet test, dotnet publish, and dotnet pack. 若要禁用隐式还原,请使用 --no-restore 选项。To disable implicit restore, use the --no-restore option.

在执行显式还原有意义的某些情况下,例如 Azure DevOps Services 中的持续集成生成中,或在需要显式控制还原发生时间的生成系统中,dotnet restore 命令仍然有用。The dotnet restore command is still useful in certain scenarios where explicitly restoring makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control when the restore occurs.

有关如何使用 NuGet 源的信息,请参阅 dotnet restore 文档For information about how to manage NuGet feeds, see the dotnet restore documentation.



    指定项目文件。Specifies the project file. 如果未指定,此命令会搜索当前目录来获取一个项目文件。If not specified, the command searches the current directory for one.


    要添加的包引用。The package reference to add.


  • -f|--framework <FRAMEWORK>

    仅在以特定框架为目标时添加包引用。Adds a package reference only when targeting a specific framework.

  • -h|--help

    打印出有关命令的简短帮助。Prints out a short help for the command.

  • --interactive

    允许命令停止并等待用户输入或操作(例如,完成身份验证)。Allows the command to stop and wait for user input or action (for example, to complete authentication). 从 .NET Core 2.1 SDK,版本 2.1.400 或更高版本开始可用。Available since .NET Core 2.1 SDK, version 2.1.400 or later.

  • -n|--no-restore

    在不执行还原预览和兼容性检查的情况下添加包引用。Adds a package reference without performing a restore preview and compatibility check.

  • --package-directory <PACKAGE_DIRECTORY>

    要在其中还原包的目录。The directory where to restore the packages. Windows 上的默认包还原位置为 %userprofile%\.nuget\packages,macOS 和 Linux 上的默认包还原位置为 ~/.nuget/packagesThe default package restore location is %userprofile%\.nuget\packages on Windows and ~/.nuget/packages on macOS and Linux. 有关详细信息,请参阅在 NuGet 中管理全局包、缓存和临时文件夹For more information, see Managing the global packages, cache, and temp folders in NuGet.

  • --prerelease

    允许安装预发行包。Allows prerelease packages to be installed. 自 .NET Core 5 SDK 起可用Available since .NET Core 5 SDK

  • -s|--source <SOURCE>

    要在还原操作期间使用的 NuGet 包源的 URI。The URI of the NuGet package source to use during the restore operation.

  • -v|--version <VERSION>

    包的版本。Version of the package. 请参阅 NuGet 包版本控制See NuGet package versioning.


  • Newtonsoft.Json NuGet 包添加到项目:Add Newtonsoft.Json NuGet package to a project:

    dotnet add package Newtonsoft.Json
  • 向项目添加特定版本的包:Add a specific version of a package to a project:

    dotnet add ToDo.csproj package Microsoft.Azure.DocumentDB.Core -v 1.0.0
  • 使用特定的 NuGet 源添加包:Add a package using a specific NuGet source:

    dotnet add package Microsoft.AspNetCore.StaticFiles -s

请参阅See also