ENVDTE - Add new project to existing solution and

2019-02-26 09:55发布

问题:

I wrote a Visual Studio Wizard Template using c# on visual studio 2012.

I followed the MSDN steps: I created a VS template, then I created a Class Library project with a class which implements the IWizard interface, I configured the .vstemplate file and etc...

In my Class Library project I copy an existing solution from some directory in my computer, I add the new generated project to that solution, and run it.

I'm doing this like:

public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            solutionDir = replacementsDictionary["$solutiondirectory$"];
            destProjectDir = replacementsDictionary["$destinationdirectory$"];
            projName = replacementsDictionary["$specifiedsolutionname$"];
            EmulationDir = @"MY_PATH\TestSln";
            DirectoryCopy(EmulationDir, solutionDir);
            dte = (DTE2)automationObject;          

        }

public void RunFinished()
        {
            Solution2 solution;
            Project p;
            solution = (Solution2)dte.Solution;
            solution.Open(solutionDir + "\\TestSln.sln");

            p = solution.AddFromFile(destProjectDir + "\\" + projName + ".vcxproj");

        }

but I have to add the new project to a specific sub-folder of the solution: the above code adds the new project to the solution straightly, and I'ld like to add it to the solutionDir\apps.

Do you know about any way to do thus? thanks!!

回答1:

You can accomplish this using SolutionFolder interface:

Project project = getSolutionSubFolder(solution, "SubFolderName");
if (project != null)
{
    SolutionFolder folder = (SolutionFolder)project.Object;
    folder.AddFromFile("yourProjectFilePath");
}

Where getSolutionSubFolder method looks like this:

private static Project getSolutionSubFolder(Solution2 solution, string subfolder)
{
    return 
        solution
            .Projects
            .Cast<Project>()
            .FirstOrDefault(
            p => string.Equals(p.Name, subfolder, StringComparison.Ordinal));
}