Shevek
Shevek

Reputation: 4053

Add a folder structure to a Visual Studio Solution

Is it possible to add a folder structure to Solution Items without manually adding each level of the tree?

We have a multi project solution which requires several third-party libraries, at least one of which has a multi-layer tree.

We have a libs folder at the solution root level, alongside all the other projects.

The answers regarding Show Hidden Files, etc. don't work for solution items, only within a project.

Is there any way to get around this?

Do we have to add them folder by folder if we want them at the solution level?

(A similar question has been answered many times regarding Visual Studio projects. However my question is about Visual Studio solutions.)

Upvotes: 17

Views: 17159

Answers (6)

Dean Wiles
Dean Wiles

Reputation: 41

I was looking for something similar and came across this thread. I had several data files that I wanted included with my solution but weren't specific to any one project in that solution.

I'm using VS 2019 and discovered they now have "Shared Projects" in C#, VB and C++ flavors. These projects don't build anything, but it's a convenient way to include some related directories/files.

  • Add a new Shared Project to your solution with the desired directory name.
  • Copy all your secondary files to the new directory.
  • Select "Show All Files" in the Solution Explorer.
  • Select the files, then right-click and select "Include in Project".
  • You can now view the file structure and open the files as part of your VS solution but VS will not try to build anything for that Shared Project.
  • If you have Source Control enabled, VS will also check-in those files (unless you mark them as excluded).

Upvotes: 4

Josh Gallagher
Josh Gallagher

Reputation: 5329

Not quite an answer to the question, but to view folders on the file system within Visual Studio Solution Explorer there is now a way of switching views between the solution and the folders. Here is the button to switch:

Visual Studio Solution Explorer showing mouse hovering over switch button in tool bar

Clicking/tapping on this brings up "Solution Explorer - Views" content showing all available solution files and option for "Folder View":

Solution Explorer Views content showing both solution folder and solution file

Double clicking the "Folder View" shows the folders in the underlying file system:

Solution Explorer Folder View showing folder in the underlying file system

Screen shots taken with Visual Studio 2019 16.10 preview 4, but this has been there for a while now.

Upvotes: 5

Andrew
Andrew

Reputation: 20061

One option is to add an extension: "Add Folder as Solution Folder"

With this extension you can do this:

right click the solution > Add > Add Folder as Solution Folder

Upvotes: 1

dba
dba

Reputation: 1175

One Option would be to write a little exe and add this as external Tool to VS. Basically you would need to edit the sln file (plain text) by adding the folders as "Projects"

...
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mycsproj", " 
[somerelativepath]\mycsproj.csproj", "{projGUID}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FolderName", 
"FolderName", "{FolderGUID}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SubFolderName", 
"SubFolderName", "{SubFolderGUID}"
EndProject
...

{FAE04..} shall be the Project Type GUID for CSproject
{2150...} would be the Project Type GUID for Project Folder

And define the hierarchy in the section

...
GlobalSection(NestedProjects) = preSolution
{projGUID} = {SubFolderGUID}
{SubFolderGUID} = {FolderGUID}
EndGlobalSection
...

Now it's up to you to write some exe, reading the folder info of the libs from the disk and write according Project-Folder Infos in the sln. This should not be a huge effort :)

So whenever you collect multiple projects to your solution, you hit your ext tool and have the connected projects structured fine.

And still I might be on a false track understanding your issue:)

Upvotes: 0

dba
dba

Reputation: 1175

If I get you right, you'd like to have the projects organized in your solution. Not sure if this fit your scenario, but if you choose a ".sln" (switch file-extension in the filebrowser-Dialog) when adding existing projects to your actual solution it will add all the project organized as they are saved in the sln to add. (VS2017)

PS: Yes, I see this is a 7 Years old post :)

Upvotes: 0

Mike Dour
Mike Dour

Reputation: 3766

Solution folders are just logical groupings of items. I don't think they relate to the file structure on your system. That is why you don't see them with a "Show hidden files" sort of functionality. You must right-click the solution, add a new folder, and then right click on the folder to add existing items or nested folders.

Upvotes: 7

Related Questions