Home About

February 24th, 2009

Show all assemblies loaded by your C# program - 3

Sometimes it is handy to have a quick overview of all the assemblies that your C# program has loaded. Maybe because you are trying to debug a version conflict, or because you want to distribute your application and want to get an idea of its dependencies.

To find the loaded assemblies we first need to determine the current AppDomain (it is possible for your application to have more than one AppDomain, but in that case you have created the second one yourself). Each AppDomain keeps a list of Assemblies it is using and in the following example code we query it using the GetAssemblies() method.

By calling FullName we obtain the name, version, culture and public key ID for each assembly.

using System;
using System.Text;
using System.Reflection;

namespace AssemblyListing
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain MyDomain = AppDomain.CurrentDomain;
            Assembly[] AssembliesLoaded = MyDomain.GetAssemblies();

            foreach (Assembly MyAssembly in AssembliesLoaded)
            {
                Console.WriteLine("Loaded: {0}", MyAssembly.FullName);
            }
        }
    }
}
Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google
  • Reddit

Tags: ,

3 Responses to “Show all assemblies loaded by your C# program”

  1. Gareth Says:

    Yep, this is well useful for debugging version conficts, especially with apps that have a lot of assemblies.

    Assuming you didn’t want to make further use of AppDomain and Assembly[], you could shorten this to:

    foreach (Assembly MyAssembly in AppDomain.CurrentDomain.GetAssemblies())
    {
    Console.WriteLine(”Loaded: {0}”, MyAssembly.FullName);
    }

    Sometimes I’ve made use of this functionality in WinForms “About” boxes, by enabling a “Show Loaded Assemblies” button if the program is running in Debug mode.

  2. Martijn Says:

    Hi Gareth,

    Thanks for the comment — I tried to keep things readable, but you are right of course.

    Cheers,
    Martijn

  3. Mark Says:

    Excellent, this was very useful. I used it to know what classes are implementing an interface in a plugin based application when the plugins are loaded by another assembly.

Leave a Reply


Recent Comments
  • Hakbor: This works fine. But to get the NUnit to use my current tests (and not the old ones) , it is not sufficient...
  • Alberto: Your plugin is very useful; I installed it on several different blogs I manage and I’m very happy with...
  • Nelson: Saved me from doing it myself. Good article.
  • andy: i am currently playing taiwanese server wow in 奈辛瓦里(PVP) and i would like to realm transfer to...
  • berties: any english speaking playing on a taiwanese server?