19

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?

Greg Gum
  • 33,478
  • 39
  • 162
  • 233
  • The closest I can get is to create a `Visual Studio Solution\blank solution`, then create projects with `add to solution` option. Each project ends up in it's own directory, such as `\example\project1`, `\example\project2`, with the solution located in `\example` . The source files can be located just about anywhere, just use `add existing item` . – rcgldr Mar 28 '17 at 02:43
  • I'm not sure if this is what you want, but from my previous comment, I could have created the projects in a sub-directory: `\example\src\project1` , `\example\src\project2`, ... . VS also ignores unused source files in a directory, so you can have collection of unused source files, and then rename or copy one of them to a project source file to work with that source file, then rename or copy the source file back after you're done working with that source file. – rcgldr Mar 28 '17 at 04:20
  • 3
    Anyone has a proper answer? I can't believe there is no easy way to do that. Most of open source github projects follow this structure. –  May 25 '18 at 12:25
  • 1
    otc 2019, vs2019, anyone has an answer? – Minh Giang Oct 17 '19 at 06:19

3 Answers3

16

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 :)

Nemanja Todorovic
  • 2,521
  • 2
  • 19
  • 30
16

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.

GregD
  • 6,860
  • 5
  • 34
  • 61
1

You can use folder view option in solution explorer and create a src folder which is mapped to file drive and then create a projects under it.

Nilesh Sawant
  • 1,406
  • 14
  • 8