dotnet restoredotnet restore

本主題適用於:✓ .NET Core 1.x SDK .NET Core 2.x SDKThis topic applies to: ✓ .NET Core 1.x SDK .NET Core 2.x SDK


dotnet restore - 還原專案的相依性和工具。dotnet restore - Restores the dependencies and tools of a project.


dotnet restore [<ROOT>] [--configfile] [--disable-parallel] [--force] [--ignore-failed-sources] [--no-cache]
    [--no-dependencies] [--packages] [-r|--runtime] [-s|--source] [-v|--verbosity] [--interactive]
dotnet restore [-h|--help]


dotnet restore 命令會使用 NuGet 來還原相依性以及專案檔中指定的專案特定工具。The dotnet restore command uses NuGet to restore dependencies as well as project-specific tools that are specified in the project file. 預設會平行執行相依性和工具的還原。By default, the restoration of dependencies and tools are executed in parallel.


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

若要還原相依性,NuGet 需要套件所在的摘要。To restore the dependencies, NuGet needs the feeds where the packages are located. 摘要通常透過 NuGet.config 組態檔提供。Feeds are usually provided via the NuGet.config configuration file. 安裝 CLI 工具時,會提供預設組態檔。A default configuration file is provided when the CLI tools are installed. 您可以在專案目錄中建立自己的 NuGet.config 檔案,以指定其他摘要。You specify additional feeds by creating your own NuGet.config file in the project directory. 您也可以在命令提示字元中針對每個引動過程指定其他摘要。You also specify additional feeds per invocation at a command prompt.

針對相依性,您可以使用 --packages 引數指定已還原套件在還原作業期間的放置位置。For dependencies, you specify where the restored packages are placed during the restore operation using the --packages argument. 如果未指定,則會使用預設的 NuGet 套件快取,它位於所有作業系統上使用者主目錄的 .nuget/packages 目錄中。If not specified, the default NuGet package cache is used, which is found in the .nuget/packages directory in the user's home directory on all operating systems. 例如,Linux 上的 /home/user1 或 Windows 上的 C:\Users\user1For example, /home/user1 on Linux or C:\Users\user1 on Windows.

針對專案特定工具,dotnet restore 會先還原在其中封裝工具的套件,然後繼續還原其專案檔中所指定的工具相依性。For project-specific tooling, dotnet restore first restores the package in which the tool is packed, and then proceeds to restore the tool's dependencies as specified in its project file.

Nuget.Config 檔案中的一些設定 (如果有的話) 會影響 dotnet restore 命令的行為。The behavior of the dotnet restore command is affected by some of the settings in the Nuget.Config file, if present. 例如,在 NuGet.Config 中設定 globalPackagesFolder 會將還原的 NuGet 套件置於指定的資料夾。For example, setting the globalPackagesFolder in NuGet.Config places the restored NuGet packages in the specified folder. 這是在 dotnet restore 命令上指定 --packages 選項的替代方式。This is an alternative to specifying the --packages option on the dotnet restore command. 如需詳細資訊,請參閱 NuGet.Config 參考For more information, see the NuGet.Config reference.

隱含 dotnet restoreImplicit dotnet restore

從 .NET Core 2.0 開始,當您發出下列命令時,dotnet restore 會於必要時隱含地執行:Starting with .NET Core 2.0, dotnet restore is run implicitly if necessary when you issue the following commands:

多數情況下,您不再需要明確地使用 dotnet restore 命令。In most cases, you no longer need to explicitly use the dotnet restore command.

有時候,可能不適合隱含執行 dotnet restoreSometimes, it might be inconvenient to run dotnet restore implicitly. 例如,某些自動化系統,像是建置系統,必須明確呼叫 dotnet restore 以控制還原發生的時間,進而控制網路使用量。For example, some automated systems, such as build systems, need to call dotnet restore explicitly to control when the restore occurs so that they can control network usage. 為避免隱含執行 dotnet restore,在使用任一這些命令時,可使用 --no-restore 旗標來停用隱含還原。To prevent dotnet restore from running implicitly, you can use the --no-restore flag with any of these commands to disable implicit restore.



要還原之專案檔的選用路徑。Optional path to the project file to restore.


--configfile <FILE>

要用於還原作業的 NuGet 組態檔 (NuGet.config)。The NuGet configuration file (NuGet.config) to use for the restore operation.


停用平行還原多個專案。Disables restoring multiple projects in parallel.


即使最後的還原成功,仍強制解析所有相依性。Forces all dependencies to be resolved even if the last restore was successful. 指定這個旗標等同於刪除 project.assets.json 檔案。Specifying this flag is the same as deleting the project.assets.json file.


印出命令的簡短說明。Prints out a short help for the command.


如果有套件符合版本需求,則只會警告有關失敗的來源。Only warn about failed sources if there are packages meeting the version requirement.


指定不要快取套件和 HTTP 要求。Specifies to not cache packages and HTTP requests.


在還原包含專案對專案 (P2P) 參考的專案時,會還原根專案,而非參考。When restoring a project with project-to-project (P2P) references, restores the root project and not the references.


指定已還原套件的目錄。Specifies the directory for restored packages.


指定套件還原的執行階段。Specifies a runtime for the package restore. 這用來針對 .csproj 檔案內 <RuntimeIdentifiers> 標記中未明確列出的執行階段還原套件。This is used to restore packages for runtimes not explicitly listed in the <RuntimeIdentifiers> tag in the .csproj file. 如需執行階段識別項 (RID) 清單,請參閱 RID 目錄For a list of Runtime Identifiers (RIDs), see the RID catalog. 多次指定這個選項來提供多個 RID。Provide multiple RIDs by specifying this option multiple times.

-s|--source <SOURCE>

指定要在還原作業期間使用的 NuGet 套件來源。Specifies a NuGet package source to use during the restore operation. 此設定會覆寫 NuGet.config 檔案中所指定的所有來源。This setting overrides all of the sources specified in the NuGet.config files. 多次指定這個選項,即可提供多個來源。Multiple sources can be provided by specifying this option multiple times.

--verbosity <LEVEL>

設定命令的詳細資訊層級。Sets the verbosity level of the command. 允許的值為 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].


允許命令停止並等候使用者輸入或動作 (例如完成驗證)。Allows the command to stop and wait for user input or action (for example to complete authentication). 自 .NET Core 2.1.400 起。Since .NET Core 2.1.400.


還原目前目錄中專案的相依性和工具︰Restore dependencies and tools for the project in the current directory:

dotnet restore

還原在指定路徑中找到之 app1 專案的相依性和工具︰Restore dependencies and tools for the app1 project found in the given path:

dotnet restore ~/projects/app1/app1.csproj

使用提供為來源的檔案路徑,還原目前目錄中專案的相依性和工具︰Restore the dependencies and tools for the project in the current directory using the file path provided as the source:

dotnet restore -s c:\packages\mypackages

使用提供為來源的兩個檔案路徑,還原目前目錄中專案的相依性和工具︰Restore the dependencies and tools for the project in the current directory using the two file paths provided as sources:

dotnet restore -s c:\packages\mypackages -s c:\packages\myotherpackages

還原目前目錄中專案的相依性和工具,並且只顯示最小輸出:Restore dependencies and tools for the project in the current directory and shows only minimal output:

dotnet restore --verbosity minimal