0

I've used dotnet cli to create a solution and subsequent projects inside. As part of the exercise i am also trying to create folders within the projects to store different type of classes.

I use mkdir to create the folder, which works however when i open VS2019 the folders don't appear in the solution.

How can i create the folders and link them to the solution?

EXAMPLE of my cli code. (This is done on Win10 machine)

dotnet new sln -n TestService -o TestService
cd TestService
dotnet new webapi -n TestAPI
cd TestAPI
mkdir TempFolder
cd ..
dotnet sln TestService.sln add TestAPI\TestAPI.csproj

Open in VS2019, the TempFolder is not in the solution view, however does exist in Folder view.

Aeseir
  • 7,754
  • 10
  • 58
  • 107
  • Visual Studio solutions don’t show the directories, you would need to specifically add them to your project. On another note, I'd suggest my IDE of choice, Visual Studio Code, you'll see the folder structure as it is, also helps for arranging projects to be filed as they are created. – Aaron Gibson Jan 28 '20 at 10:03

1 Answers1

-2

Project folders

In modern .NET Core projects (using the .NET SDK), files are automatically added based on a global file pattern. For example, any .cs file anywhere within your project directory is by default automatically configured to be a part of your project that needs to be compiled. This pattern however only applies to files, not directories.

Directories are not an explicit part of a project by default. Instead, they are only there if they are “needed” for a path to a file. That’s why you won’t see folders within Visual Studio until there is a file that is part of the project.

If you are within your project folder and then add a folder there, you will not see the folder there. But as soon as you add a file to that folder (echo '' > TempFolder\Test.cs), it should automatically be picked up by Visual Studio:

Created file appears within the TempFolder directory which was previously not visible

You can also enable the “Show all files“ option in the solution explorer, to make folders that are not part of the project appear in the solution folder:

TempFolder directory appears when choosing the “Show all files” option

As you can see, the folder appears as a transparent item because it is not part of the project itself. You can then right click on the item and choose “Include In Project“ to make this folder an explicit part of the project. This action will add the following section to the project file:

<ItemGroup>
    <Folder Include="TempFolder\" />
</ItemGroup>

This basically tells Visual Studio that the folder is part of the project even though it does not contain any files. As soon as you do add any file to the folder, Visual Studio will remove that configuration though since the folder is now again an implicit part of the project.

Solution folders

Visual Studio solutions don’t show the actual directories within your solution directory but rather a virtual directory as configured within the .sln file. Projects being located within subdirectories will not automatically be located within such a folder within the solution structure, and similarly non-project folders will also need to be added to the solution file first.

There is no mechanism to manage the solution folders with the dotnet sln command though. The only thing that you can do is add a project into a particular virtual folder within the solution:

dotnet sln add path/to/project.csproj --solution-folder VirtualFolder

This would add the project.csproj inside the VirtualFolder solution folder within the Visual Studio solution.

If you want to manage the solution folders otherwise, you should do that with Visual Studio.

poke
  • 369,085
  • 72
  • 557
  • 602