.NET CLI 總覽.NET CLI overview

本文 適用于: ✔️ .net CORE 2.1 SDK 和更新版本This article applies to: ✔️ .NET Core 2.1 SDK and later versions

.NET 命令列介面 (CLI) 是用於開發、建立、執行和發佈 .NET 應用程式的跨平臺工具鏈。The .NET command-line interface (CLI) is a cross-platform toolchain for developing, building, running, and publishing .NET applications.

.Net CLI 隨附于 .NET SDKThe .NET CLI is included with the .NET SDK. 若要瞭解如何安裝 .NET SDK,請參閱 安裝 .Net CoreTo learn how to install the .NET SDK, see Install .NET Core.

CLI 命令CLI commands

預設會安裝下列命令:The following commands are installed by default:

基本命令Basic commands

專案修改命令Project modification commands

進階命令Advanced commands

工具管理命令Tool management commands

工具是從 NuGet 套件安裝的主控台應用程式,會從命令提示字元叫用。Tools are console applications that are installed from NuGet packages and are invoked from the command prompt. 您可以自行撰寫工具或安裝協力廠商所撰寫的工具。You can write tools yourself or install tools written by third parties. 工具也稱為「通用工具」、「工具路徑工具」和「本機工具」。Tools are also known as global tools, tool-path tools, and local tools. 如需詳細資訊,請參閱 .net 工具總覽For more information, see .NET tools overview.

命令結構Command structure

CLI 命令結構包含驅動程式 ("dotnet")命令,以及可能的命令引數選項CLI command structure consists of the driver ("dotnet"), the command, and possibly command arguments and options. 您可以在大部分的 CLI 作業中看到此模式,例如從名稱為 my_app 的目錄執行以下命令,以建立新的主控台應用程式並從命令列執行它:You see this pattern in most CLI operations, such as creating a new console app and running it from the command line as the following commands show when executed from a directory named my_app :

dotnet new console
dotnet build --output /build_output
dotnet /build_output/my_app.dll


驅動器的名稱是 dotnet,且有兩個責任:執行相依於架構的應用程式或執行命令。The driver is named dotnet and has two responsibilities, either running a framework-dependent app or executing a command.

若要執行相依於架構的應用程式,請在驅動器之後指定應用程式,例如 dotnet /path/to/my_app.dllTo run a framework-dependent app, specify the app after the driver, for example, dotnet /path/to/my_app.dll. 從應用程式的 DLL 所在的資料夾執行該命令時,只要執行 dotnet my_app.dll 即可。When executing the command from the folder where the app's DLL resides, simply execute dotnet my_app.dll. 如果您想要使用特定版本的 .NET 執行時間,請使用 --fx-version <VERSION> 選項 (參閱 dotnet 命令 參考) 。If you want to use a specific version of the .NET Runtime, use the --fx-version <VERSION> option (see the dotnet command reference).

當您提供命令給驅動器時,dotnet.exe 會啟動 CLI 命令執行程序。When you supply a command to the driver, dotnet.exe starts the CLI command execution process. 例如:For example:

dotnet build

首先,驅動器會決定要使用的 SDK 版本。First, the driver determines the version of the SDK to use. 如果檔案沒有 global.js ,則會使用最新版本的 SDK。If there is no global.json file, the latest version of the SDK available is used. 這可能是預覽版或穩定的版本,視兩者中何者版本最新。This might be either a preview or stable version, depending on what is latest on the machine. 確定 SDK 版本之後,就會開始執行命令。Once the SDK version is determined, it executes the command.


此命令會執行動作。The command performs an action. 例如,dotnet build 會建置程式碼。For example, dotnet build builds code. dotnet publish 會發佈程式碼。dotnet publish publishes code. 命令是使用 dotnet {command} 慣例實作為主控台應用程式。The commands are implemented as a console application using a dotnet {command} convention.


您在命令列上傳遞的引數即為叫用命令的引數。The arguments you pass on the command line are the arguments to the command invoked. 例如,當您執行時 dotnet publish my_app.csprojmy_app.csproj 引數會指出要發行的專案,並傳遞至 publish 命令。For example, when you execute dotnet publish my_app.csproj, the my_app.csproj argument indicates the project to publish and is passed to the publish command.


您在命令列上傳遞的選項即為叫用命令的選項。The options you pass on the command line are the options to the command invoked. 例如,當您執行時 dotnet publish --output /build_output--output 選項和其值會傳遞至 publish 命令。For example, when you execute dotnet publish --output /build_output, the --output option and its value are passed to the publish command.

另請參閱See also