dotnet 命令dotnet command

此主題適用於:✓ .NET Core SDK 1.x .NET Core SDK 2.0This topic applies to: ✓ .NET Core SDK 1.x .NET Core SDK 2.x

名稱Name

dotnet - 管理 .NET 原始程式碼和二進位檔的工具。dotnet - A tool for managing .NET source code and binaries.

概要Synopsis

dotnet [command] [arguments] [--additional-deps] [--additionalprobingpath] [-d|--diagnostics] [--fx-version]
    [-h|--help] [--info] [--list-runtimes] [--list-sdks] [--roll-forward-on-no-candidate-fx] [-v|--verbosity] [--version]

說明Description

dotnet 是管理 .NET 原始程式碼和二進位檔的工具。dotnet is a tool for managing .NET source code and binaries. 會公開執行特定工作的命令,例如 dotnet builddotnet runIt exposes commands that perform specific tasks, such as dotnet build and dotnet run. 每個命令會定義自己的引數。Each command defines its own arguments. 每個命令後方鍵入 --help 以存取簡短說明文件。Type --help after each command to access brief help documentation.

可藉由指定應用程式 DLL (例如 dotnet myapp.dll),使用 dotnet 來執行應用程式。dotnet can be used to run applications, by specifying an application DLL, such as dotnet myapp.dll. 請參閱 .NET Core 應用程式部署 來了解部署選項。See .NET Core application deployment for to learn about deployment options.

選項Options

--additional-deps <PATH>

其他 deps.json 檔案的路徑。Path to additional deps.json file.

--additionalprobingpath <PATH>

包含探查原則和要探查之組件的路徑。Path containing probing policy and assemblies to probe.

-d|--diagnostics

啟用診斷輸出。Enables diagnostic output.

--fx-version <VERSION>

用來執行應用程式的 .NET Core 執行階段版本。Version of the .NET Core runtime to use to run the application.

-h|--help

印出指定命令的文件,例如dotnet build --helpPrints out documentation for a given command, such as dotnet build --help. dotnet --help 會印出可用命令的清單。dotnet --help prints a list of available commands.

--info

會印出關於 .NET Core 安裝和電腦環境的詳細資訊,例如目前作業系統和 .NET Core 版本的認可 SHA。Prints out detailed information about a .NET Core installation and the machine environment, such as the current operating system, and commit SHA of the .NET Core version.

--list-runtimes

顯示已安裝的 .NET Core 執行階段。Displays the installed .NET Core runtimes.

--list-sdks

顯示已安裝的 .NET Core SDK。Displays the installed .NET Core SDKs.

--roll-forward-on-no-candidate-fx <N>

在必要的共用架構無法使用時定義行為。Defines behavior when the required shared framework is not available. N 可以是:N can be:

  • 0 - 對次要版本也停用向前復原。0 - Disable even minor version roll forward.
  • 1 - 對次要版本向前復原,主要版本則不。1 - Roll forward on minor version, but not on major version. 這是預設行為。This is the default behavior.
  • 2 - 對次要及主要版本向前復原。2 - Roll forward on minor and major versions.

如需詳細資訊,請參閱向前復原For more information, see Roll forward.

-v|--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]. 不在每項命令中支援,請參考特定命令的頁面來判斷此選項是否可用。Not supported in every command; see specific command page to determine if this option is available.

--version

印出使用中的 .NET Core SDK 版本。Prints out the version of the .NET Core SDK in use.

dotnet 命令dotnet commands

一般General

命令Command 功能Function
dotnet builddotnet build 建置 .NET Core 應用程式。Builds a .NET Core application.
dotnet build-serverdotnet build-server 與組建所啟動的伺服器互動。Interacts with servers started by a build.
dotnet cleandotnet clean 清除組建輸出。Clean build outputs.
dotnet helpdotnet help 顯示命令更詳細的線上文件。Shows more detailed documentation online for the command.
dotnet migratedotnet migrate 將有效的 Preview 2 專案移轉至 .NET Core SDK 1.0 專案。Migrates a valid Preview 2 project to a .NET Core SDK 1.0 project.
dotnet msbuilddotnet msbuild 提供對 MSBuild 命令列的存取。Provides access to the MSBuild command line.
dotnet newdotnet new 初始化指定範本的 C# 或 F# 專案。Initializes a C# or F# project for a given template.
dotnet packdotnet pack 建立您程式碼的 NuGet 套件。Creates a NuGet package of your code.
dotnet publishdotnet publish 發行 .NET 與 Framework 相依的應用程式或獨立應用程式。Publishes a .NET framework-dependent or self-contained application.
dotnet restoredotnet restore 還原所指定應用程式的相依性。Restores the dependencies for a given application.
dotnet rundotnet run 從原始檔執行應用程式。Runs the application from source.
dotnet slndotnet sln 要在方案檔中新增、移除及列出專案的選項。Options to add, remove, and list projects in a solution file.
dotnet storedotnet store 在執行階段套件存放區中儲存組件。Stores assemblies in the runtime package store.
dotnet testdotnet test 使用測試執行器執行測試。Runs tests using a test runner.

專案參考Project references

命令Command 功能Function
dotnet add referencedotnet add reference 新增專案參考。Adds a project reference.
dotnet list referencedotnet list reference 列出專案參考。Lists project references.
dotnet remove referencedotnet remove reference 移除專案參考。Removes a project reference.

NuGet 套件NuGet packages

命令Command 功能Function
dotnet add packagedotnet add package 新增 NuGet 套件。Adds a NuGet package.
dotnet remove packagedotnet remove package 移除 NuGet 套件。Removes a NuGet package.

NuGet 命令NuGet commands

命令Command 功能Function
dotnet nuget deletedotnet nuget delete 從伺服器刪除或取消列出套件。Deletes or unlists a package from the server.
dotnet nuget localsdotnet nuget locals 清除或列出本機 NuGet 資源,例如 http-request 快取、暫時快取,或整部電腦的全域套件資料夾。Clears or lists local NuGet resources such as http-request cache, temporary cache, or machine-wide global packages folder.
dotnet nuget pushdotnet nuget push 將套件推送至伺服器並發行。Pushes a package to the server and publishes it.

通用工具命令Global Tools commands

.NET Core 通用工具將從 .NET Core SDK 2.1.300 開始提供使用:.NET Core Global Tools are available starting with .NET Core SDK 2.1.300:

命令Command 功能Function
dotnet tool installdotnet tool install 在您的電腦上安裝通用工具。Installs a Global Tool on your machine.
dotnet tool listdotnet tool list 列出在您電腦上的預設目錄或在指定路徑中目前已安裝的所有通用工具。Lists all Global Tools currently installed in the default directory on your machine or in the specified path.
dotnet tool uninstalldotnet tool uninstall 將通用工具從您的電腦解除安裝。Uninstalls a Global Tool from your machine.
dotnet tool updatedotnet tool update 更新您電腦上的通用工具。Updates a Global Tool on your machine.

其他工具Additional tools

從 .NET Core SDK 2.1.300 開始,先前僅能透過 DotnetCliToolReference 於個別專案上使用的數個工具,現在皆已做為 .NET Core SDK 的一部分提供。Starting with .NET Core SDK 2.1.300, a number of tools that were available only on a per project basis using DotnetCliToolReference are now available as part of the .NET Core SDK. 這些工具列於下列資料表中:These tools are listed in the following table:

工具Tool 功能Function
dev-certsdev-certs 建立及管理開發憑證。Creates and manages development certificates.
efef Entity Framework Core 命令列工具。Entity Framework Core command-line tools.
sql-cachesql-cache SQL Server 快取命令列工具。SQL Server cache command-line tools.
user-secretsuser-secrets 管理開發使用者祕密。Manages development user secrets.
watchwatch 啟動會在檔案變更時執行命令的檔案監看員。Starts a file watcher that runs a command when files change.

如需每個工具的詳細資訊,請鍵入 dotnet <tool-name> --helpFor more information about each tool, type dotnet <tool-name> --help.

範例Examples

建立新的 .NET Core 主控台應用程式:Creates a new .NET Core console application:

dotnet new console

還原所指定應用程式的相依性:Restore dependencies for a given application:

dotnet restore

注意

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

建置所指定目錄中的專案和其相依性:Build a project and its dependencies in a given directory:

dotnet build

執行應用程式 DLL,例如 myapp.dllRun an application DLL, such as myapp.dll:

dotnet myapp.dll

環境變數Environment variables

DOTNET_PACKAGES

主要套件快取。The primary package cache. 如果未設定,預設為 $HOME/.nuget/packages (Unix) 或 %HOME%\NuGet\Packages (Windows)。If not set, it defaults to $HOME/.nuget/packages on Unix or %HOME%\NuGet\Packages on Windows.

DOTNET_SERVICING

指定載入執行階段時,共用主機所使用的服務索引位置。Specifies the location of the servicing index to use by the shared host when loading the runtime.

DOTNET_CLI_TELEMETRY_OPTOUT

指定是否收集 .NET Core 工作使用資料,並將其傳送給 Microsoft。Specifies whether data about the .NET Core tools usage is collected and sent to Microsoft. 設為 true 以選擇加入遙測功能 (可接受的值為 true1yes)。Set to true to opt-out of the telemetry feature (values true, 1, or yes accepted). 否則,請設為 false 以選擇退出遙測功能 (可接受的值為 false0no)。Otherwise, set to false to opt into the telemetry features (values false, 0, or no accepted). 如果未設定,預設值為 false,且遙測功能會處於作用狀態。If not set, the default is false and the telemetry feature is active.

DOTNET_MULTILEVEL_LOOKUP

指定是否從全域位置解析 .NET Core 執行階段、共用架構或 SDK。Specifies whether .NET Core runtime, shared framework, or SDK are resolved from the global location. 如果未設定,則預設為 trueIf not set, it defaults to true. 設定為 false 則不會從全域位置解析,而且會有獨立模式的 .NET Core 安裝 (接受 0false 值)。Set to false to not resolve from the global location and have isolated .NET Core installations (values 0 or false are accepted). 如需多層級查閱的詳細資訊,請參閱 Multi-level SharedFX Lookup (多層級 SharedFX 查閱)。For more information about multi-level lookup, see Multi-level SharedFX Lookup.

DOTNET_ROLL_FORWARD_ON_NO_CANDIDATE_FX

如果設定為 0,將停用次要版本向前復原。Disables minor version roll forward, if set to 0. 如需詳細資訊,請參閱向前復原For more information, see Roll forward.