-->

How to add 'src' folder at solution root

2020-08-10 19:10发布

问题:

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?

回答1:

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



回答2:

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.



回答3:

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

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.