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):
How can I accomplish this using Powershell (and optionally EnvDTE)? Any example code would be appreciated. Thanks!
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With