I am developing a VS Package and part of the functionality that I need to implement is to add a file to the Solution Items of the currently open Solution.
This is exactly the same action that would be performed manually if you right-click on a Solution and choose Add > Existing Item. Then selected a file on disk.
I have taken a good look at the DTE and DTE2 interfaces and can see the operations to add and manipulate projects but there doesn't appear to be any operations for adding individual files.
Thanks.
Add files to a solution To add an item to a solution, on the context (right-click) menu of the solution node in Solution Explorer, select Add > New Item, or Add > Existing Item. A solution file is a structure for organizing projects in Visual Studio.
Adding FilesRight click the project or contained folder and choose Add | Existing Item... . Use Show All Files . Click on files or folders you would like to add to the project and choose Include In Project . Drag and drop files and folders from Windows Explorer.
Adding a Solution Item To add a new file as a solution item, start by right-clicking the solution's name in the Solution Explorer pane. Choose "Add" from the context-sensitive menu that appears.
Add a projectFrom the right-click or context menu of Solution 'QuickSolution' in Solution Explorer, select Add > New Project. A dialog box opens that says Add a new project. Enter the text empty into the search box at the top, and then select C# under Language.
Ok, I realised I could just record a Macro to capture the operation then examine the code in the VS Macro IDE.
The code required to do this is.
DTE.ItemOperations.AddExistingItem(filePath);
To do this you need to access the ProjectItems member of the Project and call AddFromFile()
ProjectItem pi = project.ProjectItems.AddFromFile(filePath);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With