Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add 'src' folder at solution root

This is going to sound like a silly question, but Visual Studio does not seem to let me do this simple organization which I see all the time on github.

I start with new empty solution.

I then want to add a "src" folder which will contain project multiple projects. If I right click and select "Add Folder" VS adds a virtual folder, not an actual folder.

If I use Windows Explorer to create a new folder in the desired location, VS ignores it and does not let me "Add to solution"

If I add a project, it adds the project at the root, not in the desired folder.

So, how do I add this folder?

like image 278
Greg Gum Avatar asked Mar 28 '17 01:03

Greg Gum


People also ask

How do I add folders to Solution Explorer Visual Studio?

Creating Solution Folders To create a folder within the solution, right-click the solution name in the Solution Explorer pane and choose "Add", then "New Solution Folder" from the context-sensitive menu. To create a subfolder, start by right-clicking an existing solution folder and then choose the same menu options.

What is src folder?

The src stands for source. The /src folder comprises of the raw non-minified code. The /src folder is used to store the file with the primary purpose of reading (and/or editing) the code. The /src folder contains all the sources, i.e. the code which is required to be manipulated before it can be used.


2 Answers

I managed to do this, although it is a bit of a "cheat".

  1. Create Solution Folder called "src"

  2. In file explorer also create folder called "src" inside your solution folder

  3. Now when you add new project to the solution, add it to the "solution_folder\src" because "src" now exists on the disk. At this point, in the solution view, this project will be actually shown outside of the "src" folder.

  4. Simply drag that project to the "src" folder in your solution structure and you will have it under "src" folder in you solution view and in the actual "src" folder on the disk.

Note that "src" folder in the solution view is not the same "src" folder on the disk, one is solution folder and the other is the actual folder in file system, but your structure from solution view will follow structure in the file system.

I hope that this helps :)

like image 161
Nemanja Todorovic Avatar answered Sep 17 '22 11:09

Nemanja Todorovic


So here is how I accomplish this in Visual Studio 2019. First here is the end result:

enter image description here

To get there requires some Command line. First open a Command window and change to your local root folder for your application. In my case it's C:\Code\NameOfCoolApp.

Once in the root folder of your application, type this in:

dotnet new sln

You should get something similar to The template "Solution File" was created successfully.

Next, we're going to stub out one of your projects in the solution, so use whatever your naming convention is here. In my case, we're going to stub out the Domain project. So from that same command window, type:

dotnet new classlib -o src/NameOfCoolApp.Domain

You should then see something like The template "Class Library" was created successfully and ending with Restore succeeded.

Next, I add a stubbed Unit Test:

dotnet new mstest -o tests/Domain.UnitTests

Following the successful message, we're then going to add the above two items to the Solution by starting with:

dotnet sln add src/NameOfCoolApp.Domain

And finally:

dotnet sln add tests/Domain.UnitTests

From here you should be able to open the Solution file with VS2019 and be good to move on with the rest of your project within Visual Studio.

like image 36
GregD Avatar answered Sep 18 '22 11:09

GregD