dotnet add packagedotnet add package

本文適用於:✓ .NET Core 1.x SDK 和更新版本This article applies to: ✓ .NET Core 1.x SDK and later versions

名稱Name

dotnet add package - 將套件參考新增至專案檔。dotnet add package - Adds a package reference to a project file.

概要Synopsis

dotnet add [<PROJECT>] package <PACKAGE_NAME> [-h|--help] [-f|--framework] [--interactive] [-n|--no-restore] [--package-directory] [-s|--source] [-v|--version]

說明Description

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 https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json
info :   OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/index.json 79ms
info :   GET https://api.nuget.org/v3-flatcontainer/newtonsoft.json/12.0.1/newtonsoft.json.12.0.1.nupkg
info :   OK https://api.nuget.org/v3-flatcontainer/newtonsoft.json/12.0.1/newtonsoft.json.12.0.1.nupkg 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" />

引數Arguments

  • PROJECT

    指定專案檔。Specifies the project file. 如果未指定,命令會在目前的目錄中搜尋一個專案檔。If not specified, the command searches the current directory for one.

  • PACKAGE_NAME

    要新增的套件參考。The package reference to add.

選項Options

  • -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.

  • -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.

範例Examples

  • 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 https://dotnet.myget.org/F/dotnet-core/api/v3/index.json