Creating Project Items Programmatically

Category: visual studio extensibility


Vlatom on Thu, 06 Jun 2013 16:06:22

I have this code 


(project is type of VSProject2)


string itemPath = currentSolution.GetProjectItemTemplate("", "csproj");  project.Project.ProjectItems.AddFromTemplate(itemPath,  name);            project.Project.Save();

for creating a project item for my solution, but it creates the item in the root of the project. Is there any way to create the item in a project's subfolder created using the  

? Thanks for help!


CoolDadTx on Thu, 06 Jun 2013 17:33:53

project.ProjectItems is the root project items so that is where things would get added.  If you want to insert the item into a subfolder then you need to retrieve the subfolder first.  Project items can represent folder, files or even virtual items.  A physical folder in a C# project can be identified by the Kind value of vsProjectItemKindPhysicalFolder.  Once you've found the folder's project item you can then add the template using the item's ProjectItems property.  Note that I haven't verified this behavior so you might need to tweak it.

Michael Taylor

Carlos J. Quintero on Thu, 06 Jun 2013 20:24:16


HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in.

Vlatom on Fri, 07 Jun 2013 19:31:59

Thanks for your help, now it works!