使用PowerShell将现有项目添加到解决方案文件夹 [英] Add an existing project to solution folder using PowerShell

查看:116
本文介绍了使用PowerShell将现有项目添加到解决方案文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PowerShell脚本来动态创建一个Visual Studio项目及其文件夹和资产,并将其添加到解决方案中. 我正在使用Visual Studio DTE.

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

我的脚本正确地创建了Subfolder \ Project3 \ Project3.csproj,我可以使用DTE将其毫无问题地添加到解决方案中.

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

但是,我想在解决方案文件夹'Subfolder'中添加Project3,所以它看起来像这样(虚拟图像,红色箭头显示了我想要Project3的位置):

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

如何使用Powershell(以及可选的EnvDTE)完成此操作?任何示例代码将不胜感激.谢谢!

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

推荐答案

SolutionFolder接口具有从文件添加"方法:

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

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

Project AddFromFile(
    string FileName
)

因此,您只需要获取解决方案文件夹的句柄.我不知道您是通过DTE添加解决方案文件夹还是已经存在.

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.

如果使用Solution2.AddSolutionFolder添加它

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
)

它返回对解决方案文件夹的引用,您可以仅调用上述方法.如果已经存在,我认为您必须使用Solution2.FindProjectItem.

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

这篇关于使用PowerShell将现有项目添加到解决方案文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆