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
  • Ales: Hi, Thanks for the code… I must say I did not experience any errors decrypting any of my messages. I even...
  • JC: Thanks very useful and well explained
  • Thomas: This is a public static class written in the C# language that does not save state. You can call into the...
  • Simon: Thank you very much for this post! It helped me essentially to overcome obstacels to work with mono!
  • Graham: This is a good research for keyboard shortcuts! Some shortcuts are also compatible for Windows OS. I have...