如何以编程方式将现有项添加到项目中?

时间:2013-08-02 14:57:08

标签: c# visual-studio

如何以编程方式将项目添加到项目中?

类似于

的东西
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);
}

我想模仿Visual Studio的Add Existing Item功能。

Add Existing Item

3 个答案:

答案 0 :(得分:3)

VisualStudio项目只是一个XML文件,因此您可以使用XDocument或XmlDocument打开它并进行编辑。

答案 1 :(得分:2)

添加对EnvDTE.dll的引用,然后使用ProjectItems.AddFromFile method
您可以在this page上阅读,如果添加对EnvDTE.dll的引用,则必须将程序集的嵌入互操作类型属性设置为false

答案 2 :(得分:0)

尝试这样的事情:

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

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