c# - IVsProject.AddItem 的使用示例

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

任何人都有 IVsProject.AddItem 的示例,到目前为止我已经完成了以下工作,但不明白如何使用 IVsProject.AddItemmsdn 确实没有任何例子。

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

scnull

最佳答案

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

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
Type: System.UInt32
[in] Identifier of the container folder for the item being added. Should be VSITEMID_ROOT or other valid item identifier. See the enumeration VSITEMID. Note that this parameter is currently ignored because only adding items as children of a project node is supported. Projects that support the notion of folders will want to add the items relative to itemidLoc.

关于c# - IVsProject.AddItem 的使用示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10779974/

相关文章:

c# - 图像渲染技术 - 用于向现有照片添加逼真的文本

visual-studio - 如何在 Visual Studio 中获得 Emacs 样式的键绑定(bind)?

c# - 运行时加载的程序集中没有用于 Generic.List 的调试可视化工具

c++ - 如何将 Z3 与 C++ 结合使用

c# - 使用 HtmlAgilityPack 的 Web 查询抛出 System.Net.WebException : The request was aborted: Could not create SSL/TLS secure channel

c# - 发送多媒体指令

C++ 链接器问题,未解析的外部符号与我自己的代码

从 Windows 而不是从 Visual Studio 运行时,C#/C++ 应用程序崩溃

c++ - 尽管有 if 语句,Visual Studio 仍尝试包含 linux header

c# - 将 WinSCP.exe 放入 WinSCP .NET 程序集的项目资源中