dotnet rundotnet run

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


dotnet run - 執行原始程式碼,而不需要有任何明確的編譯或啟動命令。dotnet run - Runs source code without any explicit compile or launch commands.


dotnet run [-c|--configuration] [-f|--framework] [--force] [--interactive] [--launch-profile] [--no-build] [--no-dependencies]
    [--no-launch-profile] [--no-restore] [-p|--project] [-r|--runtime] [-v|--verbosity] [[--] [application arguments]]
dotnet run [-h|--help]


dotnet run 命令提供方便的選項,以使用一個命令透過原始程式碼來執行應用程式。The dotnet run command provides a convenient option to run your application from the source code with one command. 可用於在命令列中快速進行反覆開發。It's useful for fast iterative development from the command line. 此命令仰賴 dotnet build 命令來建置程式碼。The command depends on the dotnet build command to build the code. 建置的任何需求 (例如必須先還原專案) 也同樣適用於 dotnet runAny requirements for the build, such as that the project must be restored first, apply to dotnet run as well.

輸出檔會寫入至預設位置,也就是 bin/<configuration>/<target>Output files are written into the default location, which is bin/<configuration>/<target>. 例如,如果您有 netcoreapp2.1 應用程式並執行 dotnet run,輸出將會放置在 bin/Debug/netcoreapp2.1 中。For example if you have a netcoreapp2.1 application and you run dotnet run, the output is placed in bin/Debug/netcoreapp2.1. 而且會視需要覆寫檔案。Files are overwritten as needed. 暫存檔案會放置在 obj 目錄中。Temporary files are placed in the obj directory.

如果專案指定多個架構,執行 dotnet run 會導致錯誤,除非使用 -f|--framework <FRAMEWORK> 選項來指定架構。If the project specifies multiple frameworks, executing dotnet run results in an error unless the -f|--framework <FRAMEWORK> option is used to specify the framework.

dotnet run 命令用於專案內容中,而非已建置的組件。The dotnet run command is used in the context of projects, not built assemblies. 如果您改為嘗試執行與 Framework 相依的應用程式 DLL,您必須不透過命令使用 dotnetIf you're trying to run a framework-dependent application DLL instead, you must use dotnet without a command. 例如,若要執行 myapp.dll,使用︰For example, to run myapp.dll, use:

dotnet myapp.dll

如需 dotnet 驅動程式的詳細資訊,請參閱 .NET Core 命令列工具 (CLI) 主題。For more information on the dotnet driver, see the .NET Core Command Line Tools (CLI) topic.

為了執行應用程式,dotnet run 命令會從 NuGet 快取解析共用執行階段之外的應用程式相依性。To run the application, the dotnet run command resolves the dependencies of the application that are outside of the shared runtime from the NuGet cache. 因為它會使用快取相依性,不建議您在生產環境中使用 dotnet run 執行應用程式。Because it uses cached dependencies, it's not recommended to use dotnet run to run applications in production. 相反地,使用 dotnet publish 命令建立部署,並部署已發佈的輸出。Instead, create a deployment using the dotnet publish command and deploy the published output.


從 .NET Core 2.0 開始,您不需要執行dotnet restore ,因為它是由需要進行還原的所有命令(例如 dotnet builddotnet run)隱含執行。Starting with .NET Core 2.0, 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 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.

此命令也支援以完整形式傳入的 dotnet restore 選項 (例如 --source)。This command also supports the dotnet restore options when passed in the long form (for example, --source). 不支援簡短形式選項,例如 -sShort form options, such as -s, are not supported.



分隔 dotnet run 的引數與執行中應用程式的引數。Delimits arguments to dotnet run from arguments for the application being run. 此分隔符號之後的所有引數會傳遞至執行的應用程式。All arguments after this delimiter are passed to the application run.

-c|--configuration {Debug|Release}

定義組建組態。Defines the build configuration. 大部分專案的預設值為 DebugThe default value for most projects is Debug.

-f|--framework <FRAMEWORK>

使用指定的架構建置並執行應用程式。Builds and runs the app using the specified framework. 架構必須在專案檔中指定。The framework must be specified in the project file.


即使最後的還原成功,仍強制解析所有相依性。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.


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

--launch-profile <NAME>

啟動應用程式時使用的啟動設定檔名稱 (如果有的話)。The name of the launch profile (if any) to use when launching the application. 啟動設定檔是在 launchSettings.json 檔案中定義,通常稱為 DevelopmentStagingProductionLaunch profiles are defined in the launchSettings.json file and are typically called Development, Staging, and Production. 如需詳細資訊,請參閱使用多個環境For more information, see Working with multiple environments.


不會在執行前建置專案。Doesn't build the project before running. 它也會隱含設定 --no-restore 旗標。It also implicit sets the --no-restore flag.


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


不會嘗試使用 launchSettings.json 來設定應用程式。Doesn't try to use launchSettings.json to configure the application.


執行命令時,不會執行隱含還原。Doesn't execute an implicit restore when running the command.

-p|--project <PATH>

指定要執行的專案檔路徑 (資料夾名稱或完整路徑)。Specifies the path of the project file to run (folder name or full path). 如果未指定,則會預設為目前目錄。If not specified, it defaults to the current directory.


指定要還原套件的目標執行階段。Specifies the target runtime to restore packages for. 如需執行階段識別項 (RID) 清單,請參閱 RID 目錄For a list of Runtime Identifiers (RIDs), see the RID catalog.

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


執行目前目錄中的專案:Run the project in the current directory:

dotnet run

執行指定的專案:Run the specified project:

dotnet run --project ./projects/proj1/proj1.csproj

執行目前目錄中的專案 (因為已使用空白的 -- 選項,所以這個範例中的 --help 引數會傳遞給應用程式):Run the project in the current directory (the --help argument in this example is passed to the application, since the blank -- option is used):

dotnet run --configuration Release -- --help

還原相依性及目前目錄中專案的工具,只會顯示最基本的輸出,然後執行專案:(.NET Core SDK 2.0 及更新版本):Restore dependencies and tools for the project in the current directory only showing minimal output and then run the project: (.NET Core SDK 2.0 and later versions):

dotnet run --verbosity m