Search code examples
c#dotnet-cli

Add folders to a c# projects using dotnet cli commands


I know this can be done via Visual Studio. But I wanted to know if there is a command or a workaround to add folders to a project. My projects are: Class Libraries a project for creating a class library that targets .NET or .NET Standard

I tried with a shell script:

mkdir DTOs;
mkdir Behavior;
mkdir Features;
mkdir Filters;
mkdir Interfaces;
mkdir Mappings;
mkdir Wrappers;

Folders are created but they are not added to the project.

enter image description here


Solution

  • Please check out the following detailed answer by poke.

    Paraphrasing it for reference here:

    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.