How to Add an Existing Item to a Project Programma

2019-01-28 13:25发布

How can I programmatically add an item to a project?

Something similar to

public void AddExistingItem(string projectPath, string existingItemPath)
{
    //I'm making up the Project class here as an example
    Project p = new Project(projectPath);
    p.AddExistingItem(existingItemPath);
}

I want to mimic Visual Studio's Add Existing Item functionality.

Add Existing Item

3条回答
地球回转人心会变
2楼-- · 2019-01-28 13:43

VisualStudio project is just a XML file so you can open it using XDocument or XmlDocument and edit.

查看更多
\"骚年 ilove
3楼-- · 2019-01-28 13:50

Add a reference to EnvDTE.dll, and then use ProjectItems.AddFromFile method
As you can read on this page, you must set the Embed Interop Types property of the assembly to false if you add a reference to EnvDTE.dll

查看更多
老娘就宠你
4楼-- · 2019-01-28 13:53

Try something like this:

public void createProjectItem(DTE2 dte)
{
    //Adds a new Class to an existing Visual Basic project.
    Solution2 soln;
    Project prj;
    soln = (Solution2)_applicationObject.Solution;
    ProjectItem prjItem;
    String itemPath;
    // Point to the first project (the Visual Basic project).
    prj = soln.Projects.Item(1);
    // Retrieve the path to the class template.
    itemPath = soln.GetProjectItemTemplate("Class.zip", "vbproj");
    //Create a new project item based on the template, in this
    // case, a Class.
    prjItem = prj.ProjectItems.AddFromTemplate(itemPath, "MyNewClass");
}

from: http://msdn.microsoft.com/en-us/library/vstudio/ms228774.aspx

查看更多
登录 后发表回答