Visual Studio Solutions Folder as real Folders

Viewed 78000

I have a Visual Studio Solution. Currently, it is an empty solution (=no projects) and I have added a few solution folders.

Solution Folders only seem to be "virtual folders", because they are not really created in the Filesystem and files inside solution folders are just sitting in the same folder as the .sln file.

Is there a setting that i've overlooked that tells Visual Studio to treat Solution Folders as "real" folders, that is to create them in the file system and move files into it when I move them inside the solution into one of those folders?

Edit: Thanks. Going to make a suggestion for VS2010 then :)

18 Answers

No special setting. I don't think it's supported.

You can create real folders in a "project" within the solution, but not in the solution itself.

In Visual Studio 2017, click on the "Solutions and Folders" icon in the Solution Explorer window. This button toggles from the virtual "solution" view into a "source view" that matches the layout of folders and files on the file system. When you add a new folder, the folder is physically created in the expected location. solutions and folders.

The chosen answer suggests it would be possible to use actual projects instead of solution folders, but does not really explain how. I guess what I'm describing here is possibly the least awkward way of achieving that... :-P

The problem with regular project files is that they eventually will be compiled by MSBUILD. And if you want have a project which only contains non-compilable files, that will be an issue.

But some time ago Visual Studio introduced a new project type: Shared Project (.shproj extension). This project type does not get compiled by default, but only when (and only if) it is referenced by another project.

So one part of the trick here is to use shared projects instead of solution folders. It's obviously possible to add a shared project that is never referenced by any other project, meaning we can avoid the issue presented above.

Then, by using <None Include="**/*" /> clause in the .shproj file, we can make it automatically reflect any new files and/or subfolders.

So basically do this:

  • Create a new folder in your solution.
  • Add a new .shproj file at the root of this new folder.
  • Reference the new .shproj in your solution.

For instance, in my case, I've created a DockerDev.shproj, so I can group some docker-related scripts that we run only in our development machines:

<?xml version="1.0" encoding="utf-8"?>
<!-- DockerDev/DockerDev.shproj -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <None Include="**/*" />
  </ItemGroup>
</Project>

This .shproj file will keep track of any file, in any subfolder of this new DockerDev folder in my solution.

As far as I could see, this solution works pretty much like what the OP requested: it will work as a non-compilable reference to a folder, and it will automatically reflect any changes made to it.

You can just sync your new solution folder nesting level and also name with the actual filesystem folder and it works like a charm!

Existing project :

  • Create the actual folder
  • Create the solution folder with the exact same name
  • Copy your project folder into the new folder (Actual file system)
  • (in solution explorer) - Righ-click on same folder
  • Add => Existing project

Add new project :

  • Create your solution folders
  • (Right-click on solution) => Add new project
  • Change the Location address under the project name you want to add, to the exact same address in your solution folders

No, it's not supported. As you suspected, solution folders are simply virtual subentries in the .sln file, nothing to do with the file system.

For C# in Visual Studio 2019 I used this way (Seems to be similar to this answer, but that didn't work at least in C# solutions)

  1. In solution explorer click on switch views

enter image description here

  1. Choose folder view

enter image description here

  1. You can add individual folders to the solution

enter image description here

  1. To get back to the regular view of solution explorer just click switch views again and choose the solution.

There seems to be a limitation using this way (comment from @montonero):

... just open a solution with multiple projects and try to move the projects to some other real folders through the folder view. The issue is VS doesn't update paths to projects in a solution file

Create an empty solution then open .sln file in an editor and put these lines of code after MinimumVisualStudioVersion

Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9D8C3BB1-AEDB-4757-8559-995D12A4E6D0}"

open the solution in vs and you should add the same folder to it now you can see the folder and add a project to it you have a real folder in windows and a virtual one in vs

be sure that you created the projects with that path

You can add real folders by choosing "Add new filter" for a Visual Studio project file. You can also do "Add new filter" under an existing folder. Once the folder is created, rename it and add source or header file or whichever suits your project. This is one way I know which lets us create real folders through the Visual Studio IDE.

The folder created underneath the solution will be virtual as said. Maybe this might be called a workaround but you can physically create the folder on disk either before or when you add new item/project and Robert should be a sibling of your dad.

ps- on closer look maybe i should explain "bob's your uncle" means your fine/sorted.

Yes, it is possible in Visual Studio 2019 for the project

Though this is an old topic, I will add my answer, because I had the same issue and searched for a solution but it seemed that everyone is 100% sure that there is no way to do it. So I started to experiment with VS 2019, tried a lot of settings, and eventually figured the way out.

1 Button :D

You only need to click 1 button - Show All Files, and you will see the physical structure of your Visual Studio Solution:
VS 2019 - Show All Files Button

Now you can add files and folders to the project and they will be added to the file system (physically)

Right-click on your project → AddNew Folder
Note that the option changed from New Filter to New Folder
VS 2019 Add Folder Menu

My recommendation for C++

  1. Create a root folder inside your project's directory which will contain all the application related stuff (code, headers, data, libs... ). I name it Project
  2. Add subfolders as you'd like to structure your code. I prefer the following layout: include, src, data, libs, etc

Recommended VS 2019 Project Structure

  1. Now setup Visual Studio to recognize these folders as headers and sources directories.
    1. Click on your project in the Solution Explorer. Note, in my case, it is CrazyDemo, not the
      Solution 'CreazyDemo' (1 of 1 project)
    2. Go to the project properties menu: ProjectProperties
    3. Open Configuration PropertiesVC++ Directories tab
    4. Edit Include Directories and set to $(ProjectDir)/Project/include;$(IncludePath)
    5. Edit Library Directories and set to $(ProjectDir)/Project/libs;$(LibraryPath)
    6. Edit Source Directories and set to $(ProjectDir)/Project/src;$(SourcePath)

VS 2019 Recommended Project Properties

  1. You may use the Scope to This option if you want to focus on 1 project. Just right-click on the Project folder and press Scope to This
    • Note that after this action, in order to open Project Properties you need to click on any of your project files (like main.cpp in the example) and then click on the editable client area (like when you want to change the code) and only after that you'll be able to see the
      ProjectCrazyDemo Properties option. [Visual Studio is Insane ‍♂️]

Finally, you may have a project like this

VS 2019 Recommended Project Example

Related