dotnet add packagedotnet add package

本文適用於:✓ .NET Core 1.x SDK 和更新版本This article applies to: ✓ .NET Core 1.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> [-h|--help] [-f|--framework] [--interactive] [-n|--no-restore] [--package-directory] [-s|--source] [-v|--version]


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 restoreIf the check passes, a <PackageReference> element is added to the project file and dotnet restore is run.


從 .NET Core 2.0 SDK 開始,您不需要執行 dotnet restore,因為所有需要進行還原的命令 (例如 dotnet newdotnet builddotnet run) 都會以隱含方式執行它。Starting with .NET Core 2.0 SDK, you 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 and dotnet run. 它在執行明確還原有意義的某些情況下仍然是有效的命令,例如 Azure DevOps Services 中的持續整合組建,或在需要明確控制還原進行時間的建置系統中。It's still a valid command in certain scenarios where doing an explicit restore makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control the time at which the restore occurs.

例如,將 Newtonsoft.Json 新增至 ToDo.csproj 會產生類似下例的輸出:For example, adding Newtonsoft.Json to ToDo.csproj produces output similar to the following example:

  Writing C:\Users\mairaw\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" />



    指定專案檔。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.

  • -s|--source <SOURCE>

    要在還原作業期間使用的 NuGet 套件來源。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