Useful Macros
Background:
I always have a difficult time remembering the various properties that can be set on an EnvDTE.Project object, and I never seem to find the right documentation, to jog my memory. Over the years the documentation has improved quite a bit. However, I still found that certain aspects of the VS automation model are readily discoverable via macros. Below are a few examples of macros that I use on a regular basis to quickly dump properties and UIHierarchyItem paths to the IDE's output window.
Solution:
' Returns a "Miscellany" OutputWindowPane
Function GetMiscDumpPane() As OutputWindowPane
Dim ow As OutputWindow = DTE.Windows.Item(Constants.vsWindowKindOutput).Object
Dim pane As OutputWindowPane
Try
pane = ow.OutputWindowPanes.Item("Miscellany")
Catch ex As Exception
pane = ow.OutputWindowPanes.Add("Miscellany")
End Try
Return pane
End Function
' Calculate the UIHierarchyItem Path (useful for retrieving a specific UIHierarchyItem)
Function CalcUIHierarchyItemPath(ByVal item As UIHierarchyItem) As String
Dim path As String = "\\" & item.Name
Try
While Not (item.Collection Is Nothing)
Try
path = path.Insert(0, "\\" & item.Name)
Catch ex As Exception
Finally
item = item.Collection.Parent
End Try
End While
Catch ex As Exception
End Try
Return path
End Function
' Recursively walk the UIHierarchyItems collection
Sub WalkUIHierarchyItems(ByVal items As UIHierarchyItems,
ByVal outputPane As OutputWindowPane)
If Not (items Is Nothing) Then
For Each item As UIHierarchyItem In items
If Not (item.UIHierarchyItems Is Nothing) Then
WalkUIHierarchyItems(item.UIHierarchyItems, outputPane)
End If
outputPane.OutputString(item.Name & " : " & CalcUIHierarchyItemPath(item) & vbCr)
Next
End If
End Sub
' List the UIHierarchyItems in the Solution Explorer Toolwindow
Sub DumpUIHierarchyItems()
Dim outputPane = GetMiscDumpPane()
outputPane.OutputString("========================================" & vbCr)
outputPane.OutputString("UIHierarchyItems in Solution Explorer" & vbCr)
outputPane.OutputString("========================================" & vbCr)
Dim uih As UIHierarchy = DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Object
WalkUIHierarchyItems(uih.UIHierarchyItems, outputPane)
End Sub
' Recursively walk the ProjectItems collection
Sub WalkProjectItems(ByVal items As ProjectItems, ByVal outputPane As OutputWindowPane)
If Not (items Is Nothing) Then
For Each item As ProjectItem In items
If Not (item.ProjectItems Is Nothing) Then
WalkProjectItems(item.ProjectItems, outputPane)
End If
outputPane.OutputString(item.Name & vbCr)
Next
End If
End Sub
' List Project Items for each loaded project
Sub DumpProjectItems()
Dim outputPane = GetMiscDumpPane()
For Each proj As Project In DTE.Solution.Projects
Try
outputPane.OutputString("========================================" & vbCr)
outputPane.OutputString("Project Items for " & proj.Name & vbCr)
outputPane.OutputString("========================================" & vbCr)
WalkProjectItems(proj.ProjectItems, outputPane)
Catch ex As Exception
End Try
Next
End Sub
' List Project.Properties for each loaded project
Sub DumpProjProperties()
Dim outputPane = GetMiscDumpPane()
For Each proj As Project In DTE.Solution.Projects
Try
outputPane.OutputString("========================================" & vbCr)
outputPane.OutputString("Project Properties for " & proj.Name & vbCr)
outputPane.OutputString("========================================" & vbCr)
For Each prop As [Property] In proj.Properties
outputPane.OutputString(prop.Name & " : ")
Try
outputPane.OutputString(prop.Value.ToString() & vbCr)
Catch ex As Exception
End Try
Next
Catch ex As Exception
End Try
Next
End Sub