Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在标签标题或源Visual Studio加载项下添加文件_C#_Visual Studio 2010_Visual Studio - Fatal编程技术网

C# 在标签标题或源Visual Studio加载项下添加文件

C# 在标签标题或源Visual Studio加载项下添加文件,c#,visual-studio-2010,visual-studio,C#,Visual Studio 2010,Visual Studio,通过以下代码,我可以向VisualStudio项目添加新文件 DTE dte = GetService(typeof(DTE)) as DTE; System.Array theProjects = (System.Array)dte.ActiveSolutionProjects; EnvDTE.Project theProject = null; if (theProjects.Length > 0)

通过以下代码,我可以向VisualStudio项目添加新文件

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

但是,如果我必须添加
头文件
,如何将其添加到
头文件
标签下。

如果我正确理解您要做的事情,我想您的意思是要将头文件添加到头文件夹中。如果它已经存在,你必须在你的项目中寻找它。也就是说,您必须循环查找带有您要查找的名称的项目项。如果还没有,可以使用ProjectItemsCollection的AddFolder添加它,该文件夹返回新创建的ProjectItem。在这两种情况下,最终都会得到一个表示headers文件夹的ProjectItem。现在,您可以将文件添加到此对象的项目项而不是项目的项目项。大概是这样的:

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");
不管怎样,我仍然认为模板可以避免你的很多努力和痛苦