IVsProject.AddItem的用法示例

时间:2012-05-28 06:01:34

标签: c# visual-studio-2010 visual-studio

任何人都有IVsProject.AddItem的示例,直到现在我已经完成了以下操作但不明白如何使用IVsProject.AddItem而且msdn没有任何示例。

private void MenuItemCallback(object sender, EventArgs e)
{
   IVsSolution solutionService = GetService(typeof(SVsSolution)) as IVsSolution;
   // get all projects in solution
   IEnumHierarchies enumHierarchies = null;
   Guid guid = Guid.Empty;
   ErrorHandler.ThrowOnFailure(solutionService.GetProjectEnum(
                                  (uint)__VSENUMPROJFLAGS.EPF_ALLINSOLUTION,
                                  ref guid,
                                  out enumHierarchies));
   //Loop all projects found
   if (enumHierarchies != null)
   {
      // Loop projects found
      IVsHierarchy[] hierarchy = new IVsHierarchy[1];
      uint fetched = 0;

      while (enumHierarchies.Next(1, hierarchy, out fetched) == VSConstants.S_OK
           && fetched == 1)
      {                   
         Guid projectGuid;
         ErrorHandler.ThrowOnFailure(hierarchy[0].GetGuidProperty(
                                        VSConstants.VSITEMID_ROOT,
                                        (int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
                                        out projectGuid));
         IVsProject project = (IVsProject)hierarchy[0];
         project.AddItem(VSConstants.VSITEMID_ROOT,
                         VSADDITEMOPERATION.VSADDITEMOP_OPENFILE,
                         1,
                         ...)

      }
   }            
}

以下内容也无法使用DTE

private void MenuItemCallback(object sender, EventArgs e)
{
   //GemFireWizardForm form = new GemFireWizardForm();
   //form.ShowDialog();
   //Get the solution service

   IVsSolution solutionService = GetService(typeof(SVsSolution)) as IVsSolution;
   // get all projects in solution
   IEnumHierarchies enumHierarchies = null;
   Guid guid = Guid.Empty;
   ErrorHandler.ThrowOnFailure(solutionService.GetProjectEnum(
                                  (uint)__VSENUMPROJFLAGS.EPF_ALLINSOLUTION,
                                  ref guid,
                                  out enumHierarchies));
   //Loop all projects found
   if (enumHierarchies != null)
   {
      // Loop projects found
      IVsHierarchy[] hierarchy = new IVsHierarchy[1];
      uint fetched = 0;
      Guid projectGuid = Guid.Empty;
      while (enumHierarchies.Next(1, hierarchy, out fetched) == VSConstants.S_OK
           && fetched == 1)
      {
         ErrorHandler.ThrowOnFailure(hierarchy[0].GetGuidProperty(
                                        VSConstants.VSITEMID_ROOT,
                                        (int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
                                        out projectGuid));
      }

      IVsHierarchy ppHierarchy = null;
      IVsSolution solution = (IVsSolution)GetService(typeof(SVsSolution));
      Int32 result = solution.GetProjectOfGuid(ref projectGuid, out ppHierarchy);

      if (ppHierarchy != null && result == VSConstants.S_OK)
      {
         Object automationObject = null;
         ppHierarchy.GetProperty(VSConstants.VSITEMID_ROOT,
                                 (Int32)__VSHPROPID.VSHPROPID_ExtObject,
                                 out automationObject);

         if (automationObject != null)
         {
            EnvDTE.SolutionClass sc = automationObject as EnvDTE.SolutionClass;
            EnvDTE.Projects projects = sc.Projects;
            EnvDTE.Project project = projects.Item(1);
            EnvDTE.ProjectItems pitems = project.ProjectItems;

            pitems.AddFromFileCopy(@"e:\avinash\test.cpp");
         }
      }
   }            
}

sc即将null

2 个答案:

答案 0 :(得分:2)

我设法使用以下代码将文件添加到当前项目的根目录:

string file = ... ; // Provide the absolute path of your file here
string[] files = new string[1] { file };
VSADDRESULT[] result = new VSADDRESULT[1];
proj.AddItem(
    VSConstants.VSITEMID_ROOT,
    VSADDITEMOPERATION.VSADDITEMOP_OPENFILE,
    file,
    1,
    files,
    IntPtr.Zero,
    result);

不幸的是,使用IVsProject.AddItem似乎无法向项目添加过滤器,或在过滤器下添加文件。确实,MSDN documentation提到了参数itemidloc

  

itemidLoc
  键入:System.UInt32
  [in]要添加的项目的容器文件夹的标识符。应该是VSITEMID_ROOT或其他有效的项目标识符。请参阅枚举VSITEMID。 请注意,此参数当前被忽略,因为仅支持将项目添加为项目节点的子项。支持文件夹概念的项目将要添加相对于itemidLoc的项目。

答案 1 :(得分:0)

我不知道为什么这不起作用,但也许你可以尝试使用MPF或VSXtra(深度创建),它们都可以帮助我们轻松管理HierachyNode。