Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 使用PowerShell将现有项目添加到解决方案文件夹_Visual Studio_Powershell_Envdte - Fatal编程技术网

Visual studio 使用PowerShell将现有项目添加到解决方案文件夹

Visual studio 使用PowerShell将现有项目添加到解决方案文件夹,visual-studio,powershell,envdte,Visual Studio,Powershell,Envdte,我正在使用PowerShell脚本动态创建Visual Studio项目,并将其文件夹和资产添加到解决方案中。 我正在使用VisualStudioDTE 我在文件系统上的目录结构如下: C:\Dir1\Dir2\Stuff | +--Stuff <-- folder | | | `Stuff.csproj <-- existing project, included in sln | +--Subfolder

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

我在文件系统上的目录结构如下:

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
C:\Dir1\Dir2\Stuff
|

+--StuffSolutionFolder界面有一个“从文件添加”方法:

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

如果将其与Solution2.AddSolutionFolder一起添加

它返回对解决方案文件夹的引用,您只需调用上面的方法即可。如果它已经存在,我认为您必须使用Solution2.FindProject

像下面这样的方法应该可以奏效。我现在没有办法尝试,所以可能需要调整

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

首先创建一个具有所需相对路径的“解决方案文件夹”。请注意,Visual Studio 2012不会创建具有相同相对路径的系统文件夹

现在,在“解决方案文件夹”中添加一个新项目,但在定义它时必须小心,系统中的相对路径与新“解决方案文件夹”的相对路径匹配。如果您想要的文件夹不存在,VS 2012现在将为新项目创建该文件夹

如果要添加具有匹配相对路径的现有文件,必须首先从VS外部在匹配系统相对路径中创建该文件。然后可以在Visual Studio中“添加现有文件”

Project AddSolutionFolder(
    string Name
)
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);