BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Reflection
.NET 1.1+

Obtaining a List of Loaded Assemblies

.NET applications are built by combining code from multiple assemblies, including custom executables and dynamic link libraries, and the assemblies provided by the .NET framework. Sometimes it can be useful to obtain a list of the loaded assemblies.

Assemblies

All executables and dynamic link libraries (DLL) that you create using the .NET framework are containers for assemblies. Assemblies hold program code, library code and resources that can be utilised by the containing assembly or, when made public, by other assemblies. When you create a new program, you can reference assemblies that are part of the .NET framework and custom assemblies containing shared code.

When you deploy software it is possible that over time the individual assemblies will be replaced. For example, a DLL may be replaced with a newer version that fixes bugs or improves performance. If a user reports a problem with your software, you may not know the versions of each assembly being used. However, if you can obtain a list of loaded assemblies you can read this information programmatically and display it, perhaps in an About box.

AppDomain.GetAssemblies

You can retrieve a list of loaded assemblies using the GetAssemblies method provided by the AppDomain class. This returns an array of Assembly objects, which can be examined using assembly reflection techniques. It is important to note that the method returns only the assemblies loaded within an application domain. Unless you specifically create additional application domains within your software this will be the desired information. If you do use additional application domains you will need to interrogate each independently and combine the results.

The AppDomain class is found in the System.Reflection namespace. For the example below, we'll also be using the System.Diagnostics namespace, so you should include the following two using directives:

using System.Reflection;
using System.Diagnostics;

Before we can use the GetAssemblies method we need an AppDomain instance. To obtain the one that is in use by the running thread we can use the static property, CurrentDomain.

The example below generates an array containing all of the assemblies loaded by the current application domain. The foreach loop iterates through those objects and outputs the name, assembly version, file version and product version for each.

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

foreach (Assembly asm in assemblies)
{
    FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
    AssemblyName asmName = asm.GetName();
    string name = asmName.Name;
    Version asmV = asmName.Version;
    string fileV = fvi.FileVersion;
    string prodV = fvi.ProductVersion;
    Console.WriteLine("{0} VERSIONS: (A){1}  (F){2}  (P){3}", name, asmV, fileV, prodV);
}

Try adding the above code to the Main method of a console application and executing the program. The results you see should be similar to those shown below, which were generated using the .NET framework version 4.5. You can see the details of the console application in the second line of the results. The other two lines show the details of the standard mscorlib and System assemblies.

mscorlib VERSIONS: (A)4.0.0.0  (F)4.0.30319.17929 built by: FX45RTMREL  (P)4.0.30319.17929
ConsoleApplication1 VERSIONS: (A)1.0.0.0  (F)1.0.0.0  (P)1.0.0.0
System VERSIONS: (A)4.0.0.0  (F)4.0.30319.17929 built by: FX45RTMREL  (P)4.0.30319.17929
19 December 2012