0

使用以下代码,我可以将新文件添加到 Visual Studio 项目中。

 DTE dte = GetService(typeof(DTE)) as DTE;
            System.Array theProjects = (System.Array)dte.ActiveSolutionProjects;
            EnvDTE.Project theProject = null;
            if (theProjects.Length > 0)
            {
                theProject = (EnvDTE.Project)(theProjects.GetValue(0));
                EnvDTE.ProjectItem projItem = null;
                projItem  = theProject.ProjectItems.AddFromFile(@"E:\Avinash\test.cpp");
            }   

但是如果我必须添加一个header file,我如何在header标签下添加它。

4

1 回答 1

1

If I correctly understand what you are trying to do, I think you mean that you want to add your header files in the header folder. If it's already there you have to look for it among the items for your project. I.e. you have to loop looking for the ProjectItem with the name you are looking for. If it's not already there, you can add it using the AddFolder of ProjectItemsCollection, which returns the newly created ProjectItem. In either case you end up with a ProjectItem representing the headers folder. Now you can add your file to the ProjectItems of this object instead of the ProjectItems of the Project. Something like this:

theProject = (EnvDTE.Project)(theProjects.GetValue(0));
EnvDTE.ProjectItem projItem = null;
EnvDTE.ProjectItem hdrProjItem = theProject.ProjectItems.AddFolder("Header files", null);
projItem  = hdrProjItem.ProjectItems.AddFromFile(@"E:\Avinash\test.cpp");

Anyway, I still think a template could avoid you much effort and pain

于 2012-05-28T21:01:56.953 回答