Skip to content

/commands/dotnet-cli/dotnet-sln-add-project

dotnet sln add: create a solution and add projects to it

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · 10.0.401updated
tool
.NET CLI
versions
.NET 8, .NET 9, .NET 10
os
linux, macos, windows

dotnet new sln -n Shop creates a solution file, and dotnet sln add src/Shop.Core src/Shop.App adds projects to it, so dotnet build, dotnet test and IDEs work on all of them at once. A solution only groups projects; to let one project use another's code, add a project reference with dotnet add App reference Lib. dotnet sln list shows what the solution contains and dotnet sln remove takes a project out again.

Create a solution with two projects

bash
dotnet new sln -n Shop > /dev/null
dotnet new classlib -o src/Shop.Core > /dev/null
dotnet new console -o src/Shop.App > /dev/null
dotnet sln add src/Shop.Core src/Shop.App | tr '\\' '/'
dotnet add src/Shop.App reference src/Shop.Core | tr '\\' '/'
echo "--- dotnet sln list"
dotnet sln list | tr '\\' '/'
echo "--- Shop.slnx"
cat Shop.slnx

Output

Project `src/Shop.Core/Shop.Core.csproj` added to the solution.
Project `src/Shop.App/Shop.App.csproj` added to the solution.
Reference `../Shop.Core/Shop.Core.csproj` added to the project.
--- dotnet sln list
Project(s)
----------
src/Shop.App/Shop.App.csproj
src/Shop.Core/Shop.Core.csproj
--- Shop.slnx
<Solution>
  <Folder Name="/src/">
    <Project Path="src/Shop.App/Shop.App.csproj" />
    <Project Path="src/Shop.Core/Shop.Core.csproj" />
  </Folder>
</Solution>

With the .NET 10 SDK the solution file is Shop.slnx; earlier SDKs create Shop.sln. Because both projects live under src, dotnet sln add placed them in a /src/ solution folder. On Windows, the commands print paths with backslashes; the example converts them to forward slashes so the output is the same everywhere.

Commands

CommandWhat it does
dotnet new sln -n ShopCreate Shop.slnx (.NET 10) or Shop.sln (earlier SDKs)
dotnet new sln --format slnForce the classic .sln format
dotnet sln add path/to/App.csprojAdd a project (a folder with one project file also works)
dotnet sln add --in-root src/AppAdd without creating a solution folder
dotnet sln add -s Tools src/GenPut the project in a named solution folder (--solution-folder)
dotnet sln listList the projects in the solution
dotnet sln remove src/AppRemove a project from the solution (the files stay)
dotnet sln migrateConvert Shop.sln to Shop.slnx
dotnet add src/App reference src/LibProject reference: App can use Lib (also dotnet reference add)

Pitfalls

  • Two solution files. dotnet sln and dotnet build without a path stop with MSB1011: more than one project or solution file when a folder holds both Shop.sln and Shop.slnx. Keep one, or pass the file name (dotnet sln Shop.slnx add ...).
  • Running a solution. dotnet run needs a project, not a solution: run it with dotnet run --project src/Shop.App, or it fails with Couldn't find a project to run.
  • Older tools and .slnx. Current Visual Studio and Rider versions read .slnx, but older IDEs, older SDKs and some build scripts expect .sln. Create the classic format with --format sln if your tooling needs it.
  • Reference cycles. dotnet add reference refuses nothing at add time, but a cycle (A references B, B references A) fails the build. Keep shared code in a library that depends on neither.

How to confirm it worked

dotnet sln list prints every project, and dotnet build Shop.slnx (or just dotnet build in the folder) builds them in dependency order. dotnet list src/Shop.App reference shows the project references of a single project.

FAQ

What is the difference between .sln and .slnx?

.slnx is the newer XML solution format: shorter, readable and easy to merge. The .NET 10 SDK creates .slnx by default; dotnet new sln --format sln still creates the classic format, and dotnet sln migrate converts an existing .sln to .slnx.

How do I add every project in a folder tree?

Pass several paths or a shell glob: dotnet sln add src/**/*.csproj in bash with globstar, or (Get-ChildItem -Recurse *.csproj) in PowerShell. Each project is added once; existing entries are skipped.

Does adding a project to the solution let other projects use its code?

No. The solution only groups projects for building and opening in an IDE. To use a library from another project, add a project reference with dotnet add App reference Lib (dotnet reference add in newer SDKs).

Sources