2013-06-04 41 views
0

我的問題和這個非常相似:How to cast ComObject to ENVDTE.Project?如何將ComObject投射到未建模項目的ENVDTE.Project?

我想處理在Visual Studio中選擇的項目項目 - >解決方案資源管理器。如果加載項目的代碼工作正常,但我有卸載的項目煩惱(他們被稱爲未建模項目(http://msdn.microsoft.com/en-us/library/hw7ek4f4%28v=vs.80%29.aspx)。

鑄造所選項目加載的項目uiItem.Object是EnvDTE.Project是好的,但如何投未建模項目 沒有「UnmodeledProject」類和鑄造uiItem.Object是項目項不起作用

這是我的代碼:?

Window solutionExplorer = mApplicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer); 
if(solutionExplorer != null) 
{ 
    UIHierarchy uiHierarchy = (UIHierarchy)solutionExplorer.Object; 
    if (uiHierarchy != null) 
    { 
     object[] selectedItems = (object[])uiHierarchy.SelectedItems; 
     foreach (UIHierarchyItem uiItem in selectedItems) 
     {        
      // Valid project 
      if (uiItem.Object is EnvDTE.Project) 
      { 
       EnvDTE.Project project = uiItem.Object as EnvDTE.Project; 
       if (project.FullName.Contains(".vdproj") || project.Kind == "{54435603-DBB4-11D2-8724-00A0C9A8B90C}") 
       { 

       } 
      } 
      else if (uiItem.Object is ProjectItem) 
      { 
       // This is never jumped... 
      } 
      else 
      { ... 

回答

0

,因爲我沒有找到這種情況的解決方案我用這一招:

string pathToVdProject = null; 
try 
{ 
    Window solutionExplorer = mApplicationObject.Windows.Item(Constants.vsWindowKindSolutionExplorer); 
    if (solutionExplorer != null) 
    { 
     UIHierarchy uiHierarchy = (UIHierarchy)solutionExplorer.Object; 
     if (uiHierarchy != null) 
     { 
      object[] selectedItems = (object[])uiHierarchy.SelectedItems; 
      foreach (UIHierarchyItem uiItem in selectedItems) 
      { 
       // Valid project 
       if (uiItem.Object is EnvDTE.Project) 
       { 
        EnvDTE.Project project = uiItem.Object as EnvDTE.Project; 
        if (project.FullName.Contains(".vdproj") || project.UniqueName.Contains(".vdproj") 
         || (String.Compare(project.Kind, ProjectsGuids.guidVdSetupProject, true) == 0))          
        { 
         // Valid Project has property FullName which is full path to .vdproj file 
         pathToVdProject = project.FullName; 
         break; 
        } 
       } 
       else if (uiItem.Object is ProjectItem) 
       { 
        // This never happens... 
       } 
       else 
       { 
        // This is a little tricky: Unmodeled Projects cannot be casted to EnvDTE.Project http://msdn.microsoft.com/en-us/library/hw7ek4f4%28v=vs.80%29.aspx 
        Solution2 solution = (Solution2)mApplicationObject.Solution; 

        // So get all projects in solution (including unmodeled) and try to find a match by name 
        foreach (Project project in solution.Projects) 
        { 
         if (project.Kind == EnvDTE.Constants.vsProjectKindUnmodeled) 
         { 
          // Unmodeled project found (Normal projects are recognized in 'uiItem.Object is EnvDTE.Project' 
          if (project.Name.Contains(uiItem.Name)) 
          { 
           // This is 'Project' for selected item 
           if (project.Name.Contains(".vdproj") || project.UniqueName.Contains(".vdproj")) 
           { 
            // Unmodeled projects does not offer property FullName and UniqueName does NOT contain full path to file! 
            FileInfo fileInfo = new FileInfo(solution.FullName); 

            // Create full path from solution (.sln) path and project relative path 
            pathToVdProject = fileInfo.DirectoryName + "\\" + project.UniqueName; 
            break; 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
0

所有納羅亞名單解決方案資源管理器中的ded/Unloaded項目將在您的EnvDTE應用程序對象中可用。沒有使用解決方案資源管理器窗口和UIHierarchy我得到了項目的細節。下面的代碼片段對我來說工作得很好。請看看天氣會適合你..

For Each item As EnvDTE.Project In mApplicationObject.Solution.Projects 
    If item.Globals Is Nothing AndAlso item.Object Is Nothing Then 
     Console.WriteLine(item.Name + " is currently unloaded!") 
    End If 
Next 
+0

沒有,這是後我不想去。我想識別所選項目的類別,但感謝您的評論。 – Slappy

相關問題