Dependency graph of Visual Studio projects

.NetVisual StudioMigrationGraphDependencies

.Net Problem Overview


I'm currently migrating a big solution (~70 projects) from VS 2005 + .NET 2.0 to VS 2008 + .NET 3.5. Currently I have VS 2008 + .NET 2.0.

The problem is that I need to move projects one by one to new .NET framework ensuring that no .NET 2.0 project references .NET 3.5 project. Is there any tool that would give me a nice graph of project dependencies?

.Net Solutions


Solution 1 - .Net

I needed something similar, but didn't want to pay for (or install) a tool to do it. I created a quick PowerShell script that goes through the project references and spits them out in a yuml.me friendly-format instead:

Function Get-ProjectReferences ($rootFolder)
{
    $projectFiles = Get-ChildItem $rootFolder -Filter *.csproj -Recurse
    $ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }

    $projectFiles | ForEach-Object {
        $projectFile = $_ | Select-Object -ExpandProperty FullName
        $projectName = $_ | Select-Object -ExpandProperty BaseName
        $projectXml = [xml](Get-Content $projectFile)
        
        $projectReferences = $projectXml | Select-Xml '//defaultNamespace:ProjectReference/defaultNamespace:Name' -Namespace $ns | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty "#text"
        
        $projectReferences | ForEach-Object {
            "[" + $projectName + "] -> [" + $_ + "]"
        }
    }
}

Get-ProjectReferences "C:\Users\DanTup\Documents\MyProject" | Out-File "C:\Users\DanTup\Documents\MyProject\References.txt"

Solution 2 - .Net

Update: ReSharper since version 8 has built-in ['View Project Dependencies'][3] feature.

ReSharper version prior to 8 has [Internal feature][1] to show dependency graphs in using yFiles viewer. See quick manual in the bottom of the post.

![enter image description here][2]

Howto

  1. Install yEd tool from here.
  2. Run VS with /resharper.internal command line argument.
  3. Go to ReSharper/Internal/Show Dependencies.
  4. Specify projects that you want to include to the 'big picture'.
  5. Uncheck 'Exclude terminal nodes...' unless you need it.
  6. Press 'Show'.
  7. Use hierarchical layout in yEd (Alt+Shift+H)
  8. Provide feedback =)

[1]: http://ipishukod.blogspot.ru/2013/02/vs-project-dependencies-viewer-one-that.html "See blog post about it" [2]: http://i.stack.imgur.com/9G3Wb.png [3]: https://www.youtube.com/watch?v=vi_HgZ9U5uA&list=UUGp4UBwpTNegd_4nCpuBcow

Solution 3 - .Net

Have you tried NDepend? It'll shows you the dependencies and you can also analyze the usability of your classes and methods.

Their website:

http://ndepend.com

Solution 4 - .Net

You can get a project dependency graph easily using Visual Studio 2010 Ultimate, scan to 5 minutes into this video to see how: http://www.lovettsoftware.com/blogengine.net/post/2010/05/27/Architecture-Explorer.aspx

In Visual Studio 2010 Ultimate: Architecture | Generate Dependency Graph | By Assembly.

Solution 5 - .Net

Made the powershell scripts .etc. posted here into a dotnet tool.

Try DependenSee

to install

  • Make sure you have dotnet 5 runtime installed

  • Make sure you have dotnet 6 runtime installed

  • run dotnet tool install dependensee --global

  • once installed, run dependensee "path/to/root/of/csproj/files" "path/to/output.html"

  • By default it doesn't include nuget packages, but can be enabled with -P switch

  • to see all options, run dependensee with no args.

HTML output looks like this

DependenSee Graph

For Continuous Integration Scenarios

There are command line options allowing you to output XML or JSON for further processing, either writing them to a file or writing to Standard Output allowing to pipe the output to other command line tools without touching the filesystem if necessary.

Solution 6 - .Net

I wrote a tool that might help you. VS Solution Dependency Visualizer analyzes project dependencies within a solution and create a dependency chart from this information, as well as a text report.

Solution 7 - .Net

I had a similar issue, but it was further complicated because several projects were referencing different versions of the same assembly.

To get an output that includes version information and checks for possible runtime assembly loading issues, I made this tool:

https://github.com/smpickett/DependencyViewer

(direct link to github release: https://github.com/smpickett/DependencyViewer/releases)

Solution 8 - .Net

You can create a dependency graph of your projects in VS 2010 Ultimate. Architecture Explorer lets you browse your solution, select projects and the relationships that you want to visualize, and then create a dependency graph from your selection.

For more info, see the following topics:

How to: Generate Graph Documents from Code: http://msdn.microsoft.com/en-us/library/dd409453%28VS.100%29.aspx#SeeSpecificSource

How to: Find Code Using Architecture Explorer: http://msdn.microsoft.com/en-us/library/dd409431%28VS.100%29.aspx

RC download: http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=457bab91-5eb2-4b36-b0f4-d6f34683c62a.

Visual Studio 2010 Architectural Discovery & Modeling Tools forum: http://social.msdn.microsoft.com/Forums/en-US/vsarch/threads

Solution 9 - .Net

To complete the eriawan answer on graphs generated by NDepend see screenshoots below. You can download and use the free trial edition of NDepend for a while.

More on NDepend Dependency Graph enter image description here

More on NDepend Dependency Matrix: enter image description here

Disclaimer: I am part of the tool team

Solution 10 - .Net

The Powershell solution is the best. I adapted it into a bash script that works on my machine (TM):

#!/bin/bash

for i in `find . -type f -iname "*.csproj"`; do
	# get only filename
	project=`basename $i`

	# remove csproj extension
	project=${project%.csproj}

	references=`cat $i | grep '<ProjectReference' | cut -d "\"" -f 2`
	for ref in $references; do
		# keep only filename (assume Windows paths)
		ref=${ref##*\\}

		# remove csproj extension
		ref=${ref%.csproj}

		echo "[ $project ] -> [ $ref ]"
	done

done

Solution 11 - .Net

You can create a nice graph of the references in your projects. I've described the way I did it on my blog http://www.mellekoning.nl/index.php/2010/03/11/project-references-in-ddd/

Solution 12 - .Net

VS 2019 has renamed dependency graph module to Code Map

here is official documentation : https://docs.microsoft.com/en-us/visualstudio/modeling/map-dependencies-across-your-solutions?view=vs-2019

Solution 13 - .Net

If you simply want a dependency graph I've found this is one of the cleanest ways to get one:

Dependency Analyser

Solution 14 - .Net

I've checked all the answers but none of the options were satisfying to me so I wrote my own tool to preview project-project dependencies.

https://github.com/Audionysos/VSProjectReferencesViewer

It's early stage but it worked for my needs :)

Solution 15 - .Net

This extended version of the PS Script from Danny Tuppeny shows both Project and External references:

Function Get-ProjectReferences($rootPath)
{
  $projectFiles = Get-ChildItem $rootPath -Filter *.csproj -Recurse
  $ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }

  $projectFiles | ForEach-Object {
    $projectFile = $_ | Select-Object -ExpandProperty FullName
    $projectName = $_ | Select-Object -ExpandProperty BaseName
    $projectXml = [xml](Get-Content $projectFile)

    $projectReferences = $projectXml | Select-Xml '//defaultNamespace:ProjectReference/defaultNamespace:Name' -Namespace $ns | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty "#text"
    $projectReferences | ForEach-Object {
        "PR:[" + $projectName + "]:[" + $_ + "]"
    }
  }

  $projectFiles | ForEach-Object {
    $projectFile = $_ | Select-Object -ExpandProperty FullName
    $projectName = $_ | Select-Object -ExpandProperty BaseName
    $projectXml = [xml](Get-Content $projectFile)

    $externalReferences = $projectXml | Select-Xml '//defaultNamespace:Reference/@Include' -Namespace $ns
    $externalReferences | ForEach-Object {
        "ER:[" + $projectName + "]:[" + $_ + "]"
    }

  }

}

Get-ProjectReferences "C:\projects" | Out-File "C:\temp\References.txt"

It will give a colon-separated file that can be opened and analysed in Excel.

Solution 16 - .Net

This extended version of the PS Script from Danny Tuppeny shows references for both csproj and vcxproj files, and also supports

-Depth - maximum dependency chain length

-Like - prints only dependency chains starting with projects with name -like $Like

-UntilLike - cuts dependency chains on projects with name -like $UntilLike

-Reverse - prints reversed dependency chains ([proj] <- [referencing proj])

[CmdletBinding()]
param (
	[Parameter(Mandatory=$false)]
	[string]$RootFolder = ".",
	[Parameter(Mandatory=$false)]
	[string]$Like = "*",
	[Parameter(Mandatory=$false)]
	[string]$UntilLike = "*",
	[Parameter(Mandatory=$false)]
	[switch]$Reverse,
	[Parameter(Mandatory=$false)]
	[int]$Depth=1
)

$arrow = if ($script:Reverse) { "<-" } else { "->" }

Function PrintTree ($projectNameToProjectNameList, $projectName, $maxDepth = 1, $prefix = "")
{
	$print = $script:UntilLike -eq "*" -or $projectName -Like $script:UntilLike
	$stop = $projectNameToProjectNameList[$projectName].count -eq 0 -or $maxDepth -eq 0 -or ($script:UntilLike -ne "*" -and $projectName -Like $script:UntilLike)
	
	if ($stop) {
		if ($print) {
			$prefix + "[$projectName]"
		}
	} else {
		$prefix += "[$projectName] $arrow "
		--$maxDepth
		$projectNameToProjectNameList[$projectName] | % { PrintTree $projectNameToProjectNameList $_ $maxDepth $prefix }
	}
}

Function Get-ProjectReferences ($rootFolder)
{
    $projectFiles = Get-ChildItem $rootFolder -Filter *.csproj -Recurse
	$projectFiles += Get-ChildItem $rootFolder -Filter *.vcxproj -Recurse
    $ns = @{ defaultNamespace = "http://schemas.microsoft.com/developer/msbuild/2003" }
	
	$projectGuidToProjectName = @{}
	$projectNameToProjectReferenceGuidList = @{}

    $projectFiles | ForEach-Object {
        $projectFile = $_ | Select-Object -ExpandProperty FullName
        $projectName = $_ | Select-Object -ExpandProperty BaseName
        $projectXml = [xml](Get-Content $projectFile)
		
		$projectGuid = $projectXml | Select-Xml '//defaultNamespace:ProjectGuid' -Namespace $ns | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty "#text" | % { $_ -as [Guid] }
		$projectGuidToProjectName[$projectGuid] = $projectName

		$projectReferenceGuids = $projectXml | Select-Xml '//defaultNamespace:ProjectReference/defaultNamespace:Project' -Namespace $ns | Select-Object -ExpandProperty Node | Select-Object -ExpandProperty "#text" | % { $_ -as [Guid] }
		if ($null -eq $projectReferenceGuids) { $projectReferenceGuids = @() }
		$projectNameToProjectReferenceGuidList[$projectName] = $projectReferenceGuids
    }

	$projectNameToProjectReferenceNameList = @{}
	foreach ($projectName in $projectNameToProjectReferenceGuidList.keys) {
		$projectNameToProjectReferenceNameList[$projectName] = $projectNameToProjectReferenceGuidList[$projectName] | % { $projectGuidToProjectName[$_] } | sort
	}
	
	if ($script:Reverse) {
		$projectReferenceNameToProjectNameList = @{}
		foreach ($projectName in $projectNameToProjectReferenceNameList.keys) {
			foreach ($projectReferenceName in $projectNameToProjectReferenceNameList[$projectName]) {
				if (!$projectReferenceNameToProjectNameList.ContainsKey($projectReferenceName)) { $projectReferenceNameToProjectNameList[$projectReferenceName] = @() } 
				$projectReferenceNameToProjectNameList[$projectReferenceName] += $projectName
			}
		}

		foreach ($projectName in $projectReferenceNameToProjectNameList.keys -Like $script:Like) {
			PrintTree $projectReferenceNameToProjectNameList $projectName $script:Depth
		}
	} else {
		foreach ($projectName in $projectNameToProjectReferenceNameList.keys -Like $script:Like) {
			PrintTree $projectNameToProjectReferenceNameList $projectName $script:Depth
		}
	}
}

Get-ProjectReferences $RootFolder

Solution 17 - .Net

If you're looking for a way that doesn't require any external tools, you can navigate to a project's obj/project.assets.json file. This file is generated during build, and has a hierarchical JSON structure of the dependencies (both project references and nuget packages).

It's useful for answering questions like "why the hell is this project DLL being pulled into the build directory?"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMigolView Question on Stackoverflow
Solution 1 - .NetDanny TuppenyView Answer on Stackoverflow
Solution 2 - .NetShkredov S.View Answer on Stackoverflow
Solution 3 - .NetEriawan KusumawardhonoView Answer on Stackoverflow
Solution 4 - .NetChris LovettView Answer on Stackoverflow
Solution 5 - .NetMadushanView Answer on Stackoverflow
Solution 6 - .NetdevioView Answer on Stackoverflow
Solution 7 - .NetPickettView Answer on Stackoverflow
Solution 8 - .NetEsther Fan - MSFTView Answer on Stackoverflow
Solution 9 - .NetPatrick from NDepend teamView Answer on Stackoverflow
Solution 10 - .NetNikolaos GeorgiouView Answer on Stackoverflow
Solution 11 - .NetHaceView Answer on Stackoverflow
Solution 12 - .NetSteven JView Answer on Stackoverflow
Solution 13 - .NetKieran BentonView Answer on Stackoverflow
Solution 14 - .NetPaweł AudionysosView Answer on Stackoverflow
Solution 15 - .NetSvein Terje GaupView Answer on Stackoverflow
Solution 16 - .NetIlyanView Answer on Stackoverflow
Solution 17 - .NetWillView Answer on Stackoverflow