How do you loop through currently loaded assemblies?

Jess Chadwick picture Jess Chadwick · Dec 20, 2008 · Viewed 36.1k times · Source

I've got a "diagnostics" page in my ASP.NET application which does things like verify the database connection(s), display the current appSettings and ConnectionStrings, etc. A section of this page displays the Assembly versions of important types used throughout, but I could not figure out how to effectively show the versions of ALL of the loaded assemblies.

What is the most effective way to figure out all currently referenced and/or loaded Assemblies in a .NET application?

Note: I'm not interested in file-based methods, like iterating through *.dll in a particular directory. I am interested in what the application is actually using right now.

Answer

Kent Boogaart picture Kent Boogaart · Dec 20, 2008

Getting loaded assemblies for the current AppDomain:

var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();

Getting the assemblies referenced by another assembly:

var referencedAssemblies = someAssembly.GetReferencedAssemblies();

Note that if assembly A references assembly B and assembly A is loaded, that does not imply that assembly B is also loaded. Assembly B will only be loaded if and when it is needed. For that reason, GetReferencedAssemblies() returns AssemblyName instances rather than Assembly instances.