2016-08-03 171 views
5

嗨我有一個問題,在Visual Studio擴展中的自定義生成任務。我需要確定我的自定義項目類型的項目。如果它們位於解決方案的根目錄下,我可以做得很好,但問題發生在解決方案文件夾內時。我可以將解決方案文件夾作爲EnvDTE.Project,但不知道如何從該文件夾中獲取項目。如何在解決方案文件夾中的VSIX項目中獲得項目

我想我可以從項目集合屬性中獲得它,但是這是null。

任何援助將不勝感激。

if (Scope == EnvDTE.vsBuildScope.vsBuildScopeSolution) 
{ 
    DTE2 dte2 = Package.GetGlobalService(typeof(EnvDTE.DTE)) as DTE2; 
    var sol = dte2.Solution; 
    EnvDTE.DTE t = dte2.DTE; 
    var x = t.Solution.Projects; 
    foreach(var proj in x) 
    { 
     try 
     { 
      var project = proj as EnvDTE.Project; 
      var guid = GetProjectTypeGuids(project); 
      if (guid.Contains("FOLDERGUID")) 
      { 
       //here is where I would get the project from the folder 
      } 

回答

10

我設法解決這個問題,多了一些研究和一些試驗和錯誤。如果任何人想出了這個問題,我改變了主代碼

if (Scope == EnvDTE.vsBuildScope.vsBuildScopeSolution) 
{ 
    errorListProvider.Tasks.Clear(); 
    DTE2 dte2 = Package.GetGlobalService(typeof(DTE)) as DTE2; 
    var sol = dte2.Solution; 
    var projs = sol.Projects; 
    foreach(var proj in sol) 
    { 
     var project = proj as Project; 
     if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder) 
     { 
      var innerProjects = GetSolutionFolderProjects(project); 
      foreach(var innerProject in innerProjects) 
      { 
       //carry out actions here. 
      } 
     } 
    } 
} 

的GetSolutionFolderForProjects的代碼是

private IEnumerable<Project> GetSolutionFolderProjects(Project project) 
{ 
    List<Project> projects = new List<Project>(); 
    var y = (project.ProjectItems as ProjectItems).Count; 
    for(var i = 1; i <= y; i++) 
    { 
     var x = project.ProjectItems.Item(i).SubProject; 
     var subProject = x as Project; 
     if (subProject != null) 
     { 
      //Carried out work and added projects as appropriate 
     } 
    } 

    return projects; 
} 

希望這有助於別人。

+0

感謝戴夫 - 我已經發現這對我的v6開發工作很有用 –

+0

偉大的工作戴夫,欣賞這項工作! – dprothero

相關問題