Reputation: 2280
According to what I can find here it is not possible to add a SolutionFolder inside a SolutionFolder:
Visual Studio 2005 and higher allows you to add folders to the solution (which are called solution folders), not only to add folders to a project (something that was already allowed by Visual Studio .NET 2002). Solution folders can be nested, and a folder that belongs to the solution (a root solution folder) is modeled as an EnvDTE.Project, so to add a child solution folder to a root solution folder you have to use the EnvDTE.Project.ProjectItems.AddFolder method. However, this method causes a NotImplementedException.
I am trying to do the same thing now - 7 years after the writing of that blogpost, in Visual Studio 2017 version 15.8.4 - and unfortunately, I get the same NotImplementedException when trying this.
Is there any other possible way of creating such a sub-solution-folder from a Visual Studio Extension?
Upvotes: 0
Views: 457
Reputation: 2280
So, it appears the trick is to get the SolutionFolder in which you want to create the subfolder as a EnvDTE.Project, then get its Object property and cast that as a SolutionFolder.
That will give you an object on which you can call "AddSolutionFolder" with a foldername.
using EnvDTE;
using EnvDTE80;
Solution2 solution = (Solution2)dte.Solution;
// Adds a SolutionFolder (in the standard way) underneath the Solution and returns
// a Project. That Project object is the same as what you would get when going
// over your solution with solution.Projects and getting the folder you need
Project solutionFolderAsProject = solution.AddSolutionFolder(folder.Name);
SolutionFolder solutionFolderAsSolutionFolder = (SolutionFolder)solutionFolderAsProject.Object;
Project subSolutionFolder = solutionFolderAsSolutionFolder.AddSolutionFolder(item.Name);
Upvotes: 1
Reputation: 76986
Visual Studio Extensions - How to create a SolutionFolder inside a SolutionFolder?
Here is a extension about how to create a Solution Folder from a selected folder also including the files in that selected folder: Folder To Solution Folder.
Remove the hassle of adding several files to solution folder. Just use the context menu for the solution and just below the option of creating a new solution folder you now find 'Add Folder as Solution Folder'. This will create a solution folder with the same name as you selected and add the items inside of that folder to the solution folder. This will not move the files on disk.
You can check the source code from: https://github.com/ceciliasharp/Extension.FolderToSolutionFolder
Hope this helps.
Upvotes: 1