Add an existing project to solution folder using P

2019-04-06 06:44发布

问题:

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!

回答1:

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);


回答2:

First create a 'Solution Folder' with the desired relative path. Note that Visual Studio 2012 does not create a system folder with the same relative path.

Now inside that 'Solution Folder' add a new project, but you must be careful when defining it that the relative path in the system matches the relative path of your new 'Solution Folder'. If the folder you want does not exist, VS 2012 will now create it for the new project.

If you want to add an existing file with the matching relative path, you must first create the file in the matching system relative path, from outside of VS. Then you can 'add existing file' in Visual Studio.