/flags/dotnet-cli/dotnet-run-project
dotnet run --project PATH: run a project from another directory
- tool
- .NET CLI
- command
- dotnet run
- flag
- --project PATH
- default
- the single project file in the current directory
dotnet run looks for exactly one project file in the current directory. From a solution folder or the repository root it fails with Couldn't find a project to run. dotnet run --project src/Api points it at the project folder (or .csproj file) instead, builds it and starts it, so you do not have to cd first. Program arguments still go after --.
From the parent folder
dotnet new console -o app > /dev/null
cat > app/Program.cs <<'EOF'
Console.WriteLine($"args: {string.Join(" ", args)} | cwd: {Path.GetFileName(Environment.CurrentDirectory)}");
EOF
echo "--- dotnet run"
dotnet run 2>&1 | head -1
echo "--- dotnet run --project app"
dotnet run --project app
echo "--- dotnet run --project app/app.csproj -- --port 8080"
dotnet run --project app/app.csproj -- --port 8080
echo "--- dotnet run -p app"
dotnet run -p app 2>&1 | head -2Output
--- dotnet run Couldn't find a project to run. Ensure a project exists in /tmp/work, or pass the path to the project using --project. --- dotnet run --project app args: | cwd: work --- dotnet run --project app/app.csproj -- --port 8080 args: --port 8080 | cwd: work --- dotnet run -p app Warning NETSDK1174: The abbreviation of -p for --project is deprecated. Please use --project. args: | cwd: work
Without the option the run fails with the message explained in Couldn't find a project to run. With it the program starts, and the working directory is still the folder you ran the command from (work here), not the project folder.
Forms and related options
| Command | Effect |
|---|---|
| dotnet run --project src/Api | Run the only project file in src/Api |
| dotnet run --project src/Api/Api.csproj | Name the file explicitly (needed when the folder has several) |
| dotnet run --project src/Api -- --port 8080 | Arguments after -- go to the program |
| dotnet run --project src/Api -c Release | Combine with any build option |
| dotnet run --project src/Api --launch-profile https | Choose a launchSettings.json profile |
| dotnet run --project src/Api --no-build | Run the last build without compiling |
| dotnet run app.cs | File-based app (.NET 10): run a single C# file without a project |
Working directory and content files
dotnet run --project does not change the current directory. Code that opens relative paths (File.ReadAllText("appsettings.json")) then looks in the folder you started from. ASP.NET Core uses the content root, which dotnet run sets to the project directory, so appsettings.json is found; plain console apps should build paths from AppContext.BaseDirectory instead.
In a solution
dotnet new sln -n Shop
dotnet sln add src/Api src/Worker
dotnet build # builds every project of the solution
dotnet run --project src/Api # starts one of themAdding projects to a solution is covered in dotnet sln add.
Pitfalls
-pas an abbreviation of--projectis deprecated: it still works but printsWarning NETSDK1174, as the last run shows, and it is easily confused with-p:Name=Value, the MSBuild property syntax of other dotnet commands. Spell out--project.- A folder with two project files needs the file name, otherwise dotnet run reports that it found several projects.
- Class library projects cannot be run; point
--projectat the executable project (OutputTypeExe).
FAQ
How do I pass arguments to my program with dotnet run?
Put them after a double dash: dotnet run --project src/App -- --port 8080. Everything after -- goes to the program's args; options before it belong to dotnet run.
Can dotnet run run a whole solution?
No. dotnet run needs one executable project. In a solution folder, pass the project to start with --project; build the whole solution with dotnet build instead.
Which launch profile does dotnet run use with --project?
The first profile in that project's Properties/launchSettings.json, as when you run from inside the folder. Choose another with --launch-profile NAME or skip them with --no-launch-profile.