Skip to content

/flags/dotnet-cli/dotnet-run-project

dotnet run --project PATH: run a project from another directory

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · 10.0.401updated
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

bash
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 -2

Output

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

CommandEffect
dotnet run --project src/ApiRun the only project file in src/Api
dotnet run --project src/Api/Api.csprojName the file explicitly (needed when the folder has several)
dotnet run --project src/Api -- --port 8080Arguments after -- go to the program
dotnet run --project src/Api -c ReleaseCombine with any build option
dotnet run --project src/Api --launch-profile httpsChoose a launchSettings.json profile
dotnet run --project src/Api --no-buildRun the last build without compiling
dotnet run app.csFile-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

bash
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 them

Adding projects to a solution is covered in dotnet sln add.

Pitfalls

  • -p as an abbreviation of --project is deprecated: it still works but prints Warning 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 --project at the executable project (OutputType Exe).

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.

Sources