Saturday, 7 September 2013

In a VS extension, how do I create nested solution folders?

In a VS extension, how do I create nested solution folders?

Given a set of partial project paths like this:
A\B\C\X.proj
A\B\D\Y.proj
I want to programatically reflect that structure in the solution, e.g:
- MySolution
- A
- B
- C
- X
- D
- Y
I have manage to cobbled something together, using a combination of the
following:
folderNames = folderPath.Split("\\"); // get the path components
// Find an existing solution folder
Solution.Projects.Cast<Project>().Where(p => p.Name == folderName);
Project.ProjectItems.Cast<ProjectItem().Where(p => p.Name == folderName);
Project.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}" // Is this
project a solution folder?
// Add a new solution folder
Solution2.AddSolutionFolder(folderName);
((SolutionFolder)ProjectItem.Object).AddSolutionFolder(folderName);
My algorithm works, but I don't love it. The asymmetry between Solution
and Project mean I have to duplicate the structure of the algorithm.
Is there a an easy way to do this without writing a bunch of code? Is
there an API that will do something like
Solution.GetOrAddSolutionFolder("A\B\C").AddFromFile("X.proj")

No comments:

Post a Comment