you can add a .csproj to a .sln file easily with the dotnet sln add command:

dotnet sln todo.sln add todo-app/todo-app.csproj

if you want to place the project under a specific solution folder (like how Visual Studio organizes projects within folders in the Solution Explorer), you’ll need to use an additional option that isn’t directly available in the standard dotnet CLI.
Here’s a workaround using the dotnet sln CLI:

Step-by-Step Solution:

  • Manually create the solution folder structure inside your .sln file using a text editor or Visual Studio (if possible).
  • Use the dotnet sln add command along with a workaround using Visual Studio commands (devenv) if you’re on Windows, or manually move the project in the .sln file.

Option 1: Manually Edit .sln File (Cross-platform workaround)

After running the command:
dotnet sln todo.sln add todo-app/todo-app.csproj

Open todo.sln in a text editor and look for the project entry:
Project("{GUID}") = "todo-app", "todo-app/todo-app.csproj", "{PROJECT_GUID}"
EndProject

To place it under a solution folder, first declare a solution folder:
Project("{66A26720-8FB5-11D2-AA7E-00C04F688DDE}") = "MySolutionFolder", "MySolutionFolder", "{FOLDER_GUID}"
EndProject

Then add the nesting relationship in the GlobalSection(NestedProjects) part of the file:

Option 2: Use Visual Studio (Windows Only)

If you’re on Windows, you can use the devenv command line to open the solution in Visual Studio and add the project to a solution folder there.

Summary

While the .NET CLI (dotnet sln add) does not support adding a project under a solution folder directly, you can work around this limitation by manually editing the .sln file or using Visual Studio.

Need Help With .Net Development?

Work with our skilled .Net developers to accelerate your project and boost its performance.

Hire .Net Developers

Support On Demand!

Related Q&A