2012-05-28 15 views
1

任何人都有IVsProject.AddItem的例子,直到現在我已經做了如下但不知道如何使用IVsProject.AddItemmsdn沒有任何例子。以下使用範例IVsProject.AddItem

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

+0

您是否將此開發爲一個AddIn的一部分? –

+0

你的示例代碼真的幫了我很多關於'project.AddItem'的信息,但是你不應該縮短它,因爲我必須找出缺失的參數。 – musefan

回答

0

我不知道爲什麼,這並不工作,但也許你可以嘗試使用MPF或VSXtra (由深度潛水創建),它們都幫助我們輕鬆管理HierachyNode。

1

我設法將文件添加到當前項目的根目錄,使用這段代碼:

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的項目。