How do I programmatically add a file to a Solution?

和自甴很熟 提交于 2019-12-06 02:27:56

问题


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.


回答1:


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



回答2:


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

ProjectItem pi = project.ProjectItems.AddFromFile(filePath);



回答3:


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



来源:https://stackoverflow.com/questions/11933855/how-do-i-programmatically-add-a-file-to-a-solution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!