dotnet rundotnet run

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

名稱Name

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

概要Synopsis

dotnet run [-c|--configuration <CONFIGURATION>] [-f|--framework <FRAMEWORK>]
    [--force] [--interactive] [--launch-profile <NAME>] [--no-build]
    [--no-dependencies] [--no-launch-profile] [--no-restore]
    [-p|--project <PATH>] [-r|--runtime <RUNTIME_IDENTIFIER>]
    [-v|--verbosity <LEVEL>] [[--] [application arguments]]

dotnet run -h|--help

描述Description

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 命令列工具 (CLI) 主題。For more information on the dotnet driver, see the .NET 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.

隱含還原Implicit restore

您不必執行, dotnet restore 因為它是由所有需要進行還原的命令(例如 dotnet newdotnet builddotnet run 、、 dotnet test dotnet publishdotnet pack )隱含地執行。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, dotnet run, dotnet test, dotnet publish, and dotnet pack. 若要停用隱含還原,請使用 --no-restore 選項。To disable implicit restore, use the --no-restore option.

dotnet restore 明確還原的特定情況下,此命令仍很有用,例如 Azure DevOps Services 中的持續整合組建 ,或在需要明確控制還原進行時的組建系統。The dotnet restore command is still useful in certain scenarios where explicitly restoring makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control when the restore occurs.

如需有關如何管理 NuGet 摘要的詳細資訊,請參閱 dotnet restoreFor information about how to manage NuGet feeds, see the dotnet restore documentation.

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

選項Options

  • --

    分隔 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 <CONFIGURATION>

    定義組建組態。Defines the build configuration. 大部分專案的預設值為 Debug ,但您可以覆寫專案中的組建設定。The default for most projects is Debug, but you can override the build configuration settings in your project.

  • -f|--framework <FRAMEWORK>

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

  • --force

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

  • -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 3.0 SDK 起提供。Available since .NET Core 3.0 SDK.

  • --launch-profile <NAME>

    啟動應用程式時使用的啟動設定檔名稱 (如果有的話)。The name of the launch profile (if any) to use when launching the application. 啟動設定檔是在檔案的 launchSettings.js 中定義,通常稱為 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.

  • --no-build

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

  • --no-dependencies

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

  • --no-launch-profile

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

  • --no-restore

    執行命令時,不會執行隱含還原。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.

  • -r|--runtime <RUNTIME_IDENTIFIER>

    指定要還原套件的目標執行階段。Specifies the target runtime to restore packages for. 如需執行階段識別項 (RID) 清單,請參閱 RID 目錄For a list of Runtime Identifiers (RIDs), see the RID catalog. -r 自 .NET Core 3.0 SDK 起提供的簡短選項。-r short option available since .NET Core 3.0 SDK.

  • -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]. 預設值是 mThe default value is m. 自 .NET Core 2.1 SDK 起提供。Available since .NET Core 2.1 SDK.

範例Examples

  • 執行目前目錄中的專案: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
    
  • 還原目前目錄中專案的相依性和工具,只會顯示最基本的輸出,然後執行專案:Restore dependencies and tools for the project in the current directory only showing minimal output and then run the project:

    dotnet run --verbosity m