5

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.

Martyn
  • 1,446
  • 2
  • 19
  • 30

3 Answers3

11

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);
Martyn
  • 1,446
  • 2
  • 19
  • 30
  • How does one actually deliver the file in the vstemplate? Is it possible? – tofutim Dec 11 '13 at 10:36
  • @tofutim I'm not sure exactly what your comment is asking. This question doesn't have anything to do with VS Templates. Perhaps you need to start your own specific question? – Martyn Dec 12 '13 at 13:43
1

To do this you need to access the ProjectItems member of the Project and call AddFromFile()

ProjectItem pi = project.ProjectItems.AddFromFile(filePath);
the_mandrill
  • 29,792
  • 6
  • 64
  • 93
  • Thanks for the reply. I'm trying to add the item to the Solution and not to a specific project – Martyn Aug 13 '12 at 12:06
  • Solution has [AddFromFile](http://msdn.microsoft.com/en-us/library/envdte.solution_members%28v=vs.90%29) too – the_mandrill Aug 13 '12 at 12:11
  • 1
    No that call doesn't work. It expects a Project to be provided at the path you give. I've just figured it out. See my answer above. – Martyn Aug 13 '12 at 12:15
-1

You need to AddFromFile to the ProjectItems collection of a Project

http://msdn.microsoft.com/en-us/library/envdte.projectitems.addfromfile(v=vs.100).aspx

edit:

To Add to a Solution, AddFromFile against the Solution

podiluska
  • 50,950
  • 7
  • 98
  • 104