Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an existing project to solution folder using PowerShell

I'm working on a PowerShell script to dynamically create and add a Visual Studio project with its folders and assets to a solution. I'm using Visual Studio DTE.

My directory structure on the file system is the following:

C:\Dir1\Dir2\Stuff
|
+--Stuff                  <-- folder
|  |
|  `Stuff.csproj          <-- existing project, included in sln
|
+--Subfolder              <-- Subfolder in which I want to include my new csproj
|  +--Project1            <-- folder
|  |  |
|  |  `Project1.csproj    <-- existing project, included in sln
|  |
|  +--Project2            <-- folder
|  |  |
|  |  `Project2.csproj    <-- existing project, included in sln
|  |
|  `--Project3            <-- this, subs below and csproj are created from my script
|     |
|     `Project3.csproj
|
 `Stuff.sln

My script creates Subfolder\Project3\Project3.csproj correctly, and I can add it to the solution without any problems, using DTE.

I want, however, to add Project3 in the solution folder 'Subfolder', so it looks like this (dummy image, red arrow shows where I want to have Project3):

image

How can I accomplish this using Powershell (and optionally EnvDTE)? Any example code would be appreciated. Thanks!

like image 785
Stanislav Nedelchev Avatar asked Jul 11 '12 14:07

Stanislav Nedelchev


People also ask

How do I add a project to an existing solution?

To add an existing project to a solutionIn Solution Explorer, select the solution. On the File menu, point to Add, and click Existing Project. In the Add Existing Project dialog box, locate the project you want to add, select the project file, and then click Open. The project is added to the selected solution.

How do I add a file to a Visual Studio solution?

Add files to a solution To add an item to a solution, on the context (right-click) menu of the solution node in Solution Explorer, select Add > New Item, or Add > Existing Item. A solution file is a structure for organizing projects in Visual Studio.

How do I add folders to Solution Explorer Visual Studio?

To create a folder within the solution, right-click the solution name in the Solution Explorer pane and choose "Add", then "New Solution Folder" from the context-sensitive menu. To create a subfolder, start by right-clicking an existing solution folder and then choose the same menu options.


1 Answers

The SolutionFolder interface has an "add from file" method:

http://msdn.microsoft.com/en-us/library/envdte80.solutionfolder.addfromfile

Project AddFromFile(
    string FileName
)

So you just need to get a handle to the solution folder. I don't know if you are adding the solution folder through the DTE or it already exists.

If you add it with Solution2.AddSolutionFolder

http://msdn.microsoft.com/en-us/library/envdte80.solution2.addsolutionfolder%28v=vs.110%29.aspx

Project AddSolutionFolder(
    string Name
)

It returns a reference to the solution folder and you can just call the above method. If it already exists, I think you'll have to use Solution2.FindProjectItem.

http://msdn.microsoft.com/en-us/library/2zszfd26%28v=vs.110%29.aspx

Something like the following should work. I don't have a way to try it out at the minute so tweaking might be necessary.

Solution solution = System.Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.Solution")) as EnvDTE.Solution;
Solution2 sol2 = solution as Solution2;
sol2.Create(solutionPath, solutionName);

Project folder = sol2.AddSolutionFolder("Subfolder");

folder.AddFromFile(pathToProject);
like image 170
David Mason Avatar answered Sep 20 '22 20:09

David Mason