Ivan Mitev In The Software Trenches

Technology weblog on .NET development and other things that make the world go round

October 23, 2008

VS.NET Macro To Display All File References in a VS.NET Solution

The problem: You have a large solution (mine is with 154 projects) and you want to quick find if you use file references and to what assemblies are they).

The solution: see the files below. Add the following module and class and execute ShowAllFileReferencesGroupedByProject()

Module:

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports System.Collections.Generic

Imports System.Diagnostics

Imports System.Text

Imports System.Windows.Forms

 

Public Module VsProjectsManagement

 

    Public Sub ShowAllFileReferencesGroupedByProject()

        Dim sb As New StringBuilder()

 

        Dim finder As New FileReferencesFinder()

        Dim fileReferences As Dictionary(Of VSLangProj.VSProject, List(Of String)) = finder.GetAllFileReferencesInSolution(True)

        Dim sortedProject As New List(Of VSLangProj.VSProject)(fileReferences.Keys)

        sortedProject.Sort(AddressOf FileReferencesFinder.CompareProjectsByName)

 

        For Each proj As VSLangProj.VSProject In fileReferences.Keys

            sb.Append(proj.Project.Name).Append(" : ").AppendLine()

            For Each reference As String In fileReferences.Item(proj)

                sb.AppendLine(reference)

            Next

            sb.AppendLine()

        Next

 

        Debug.Write(sb.ToString)

        MessageBox.Show(sb.ToString, "File references")

 

    End Sub

Class FileReferencesFinder:

Imports System

Imports EnvDTE

Imports EnvDTE80

Imports System.Collections.Generic

Imports System.Diagnostics

Imports System.Text

Imports System.Windows.Forms

 

Public Class FileReferencesFinder

 

    Private Function GetAllSolutionVsProjects()

        Dim vsProjects As New List(Of VSLangProj.VSProject)

        For Each proj As Project In DTE.Solution.Projects

            If TypeOf proj.Object Is VSLangProj.VSProject Then

                vsProjects.Add(proj.Object)

            End If

        Next

 

        Return vsProjects

    End Function

 

    Public Shared Function CompareProjectsByName(ByVal prj1 As VSLangProj.VSProject, ByVal prj2 As VSLangProj.VSProject) As Integer

        Dim name1 As String = prj1.Project.Name

        Dim name2 As String = prj2.Project.Name

        Return String.Compare(name1, name2)

 

    End Function

 

    Public Function GetAllFileReferencesInSolution(ByVal ignoreNetFrameworkAssemgblies As Boolean)

 

        Dim fileReferences As New Dictionary(Of VSLangProj.VSProject, List(Of String))

 

        For Each vsProj As VSLangProj.VSProject In GetAllSolutionVsProjects()

            Dim references As List(Of String) = GetProjectFileReferences(vsProj, ignoreNetFrameworkAssemgblies)

            If references.Count > 0 Then

                fileReferences.Add(vsProj, references)

            End If

        Next

 

        Return fileReferences

    End Function

 

    Private Function GetProjectFileReferences(ByVal vsProj As VSLangProj.VSProject, ByVal ignoreNetFrameworkAssemgblies As Boolean)

        Dim fileReferences As New List(Of String)

 

        For Each objReference As VSLangProj.Reference In vsProj.References

            If objReference.Type = VSLangProj.prjReferenceType.prjReferenceTypeAssembly Then

                If objReference.SourceProject Is Nothing Then

                    If ignoreNetFrameworkAssemgblies Then

                        If objReference.Path.StartsWith("C:\Windows\Microsoft.NET\Framework\") Then

                            Continue For

                        End If

 

                        If objReference.Path.StartsWith("C:\Program Files\Reference Assemblies\Microsoft\Framework\") Then

                            Continue For

                        End If

                    End If

 

                    fileReferences.Add(objReference.Path)

                End If

            End If

        Next

 

        fileReferences.Sort()

        Return fileReferences

    End Function

 

End Class