VS2022 - Can we Add Items directly to the Solution from a Project Template Wizard?

Jaeden Ruiner 126 Reputation points
2024-02-04T20:35:28.37+00:00

Working on a project template that includes a few files that need to be at the root level of the solution folder. This part isn't so difficult as i can move the files manually after generation, and update the Project.ProjectItems from within the wizard.
However, i would still like the files to be visible within the IDE so that the developer can easily find and modify them after generation. Now, I can "right click" on the solution in the IDE and manually add the three files, however, it would be nice if i could modify the solution file directly.
However, I have tried directly modifying the sln file in both the IWizard.ProjectFinishedGenerating and IWizard.RunFinished events. Additionally, i have tried using the the Project.DTE.Solution property to "add" these "Solution Items" to the solution, but none appear to be effective.
Is there a way I can have a project template evaluate the solution in order to add specific file items to the solution for the new project. Thanks Jaeden "Sifo Dyas" al'Raec Ruiner

Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
228 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jaeden Ruiner 126 Reputation points
    2024-02-05T04:09:28.1533333+00:00

    I found that by using this method, i was able to mostly achieve my goal:

    //Assuming in the RunWizard method of IWizard, the provided applicationObject parameter has
    //been cast into a class field _dte of the type EnvDTE80.DTE2
    Solution2 sln = (Solution2)this._dte.Solution;
    if (sln != null) 
    {
    	Project folder = sln.AddSolutionFolder("Solution Items");
    	foreach(string name in [FileNamesToAdd])
    	{
    		folder.ProjectItems.AddFromFile(Path.Combine([SolutionFolderFromReplacements], name);
    	}
    	foreach (Document doc in this.Application.Documents)
    	{
    		if ([FileNamesToAdd].Contains(doc.Name))
    			doc.Close();
    	}
    }
    
    //This solution does not close any external editors for files added to solution
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.