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);
            }
        }
    }
}
Be Sociable, Share!

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.


Most popular

    Sorry. No data so far.

Recent Comments
  • ARS: great plugin! I love it! but, it will be so nice if you can add attribute ‘title’ as one of...
  • 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 somewhere there...
  • berties: any english speaking playing on a taiwanese server?
  • web application development: has C# search volume really so constant over the years? really surprising.