How to Get all loaded assemblies in C#

By FoxLearn 2/4/2025 7:08:22 AM   102
In C#, you can get all loaded assemblies by using the AppDomain.CurrentDomain.GetAssemblies() method.

This will return all assemblies that are currently loaded into the application's domain.

using System;
using System.Reflection;

class Program
{
    static void Main(string[] args)
    {
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            Console.WriteLine($"Name: {assembly.GetName().Name}");
            Console.WriteLine($"Version: {assembly.GetName().Version}");
            Console.WriteLine($"Location: {assembly.Location}");
            Console.WriteLine();
        }
    }
}

The output will list all the loaded assemblies in the current domain, for example:

Name: System.Private.CoreLib
Version: 4.0.0.0
Location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0\System.Private.CoreLib.dll

Name: System.Runtime
Version: 4.2.2.0
Location: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.0\System.Runtime.dll

Name: MyApp
Version: 1.0.0.0
Location: D:\Projects\MyApp\bin\Debug\net5.0\MyApp.dll

Get custom assembly attributes

You can use assembly.GetCustomAttributesData() to retrieve all custom attribute values, as shown below:

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
	Console.WriteLine(assembly.FullName);

	foreach (var attribute in assembly.GetCustomAttributesData())
	{
		Console.WriteLine(attribute);
	}
	Console.WriteLine();
}

Add your own custom assembly metadata

You can use the AssemblyMetadata attribute to add custom metadata to your assembly.

using System.Reflection;

[assembly: AssemblyMetadata("author", "John Doe")]
[assembly: AssemblyMetadata("project", "WeatherApp")]

Get a specific assembly attribute

To retrieve these attributes, you can use the GetCustomAttributes<AssemblyMetadataAttribute>() method as shown below:

using System.Reflection;

static void Main(string[] args)
{
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        var name = assembly.GetName();
        Console.WriteLine($"Name={name.Name} Version={name.Version} Location={assembly.Location}");

        var customMetadataList = assembly.GetCustomAttributes<AssemblyMetadataAttribute>() ?? Enumerable.Empty<AssemblyMetadataAttribute>();

        foreach (var customMetadata in customMetadataList)
        {
            Console.WriteLine($"{customMetadata.Key}={customMetadata.Value}");
        }

        Console.WriteLine();
    }      
}

This outputs the custom metadata:

Name=WeatherApp Version=1.2.0.0 Location=D:\Projects\WeatherApp\bin\Debug\net5.0\WeatherApp.dll
author=John Doe
project=WeatherApp

Filter out system assemblies

To filter out system assemblies that are loaded as part of .NET, you can check the company name attribute.

using System.Reflection;

foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
    if (assembly.GetCustomAttribute<AssemblyCompanyAttribute>().Company != "Microsoft Corporation")
    {
        var name = assembly.GetName();
        Console.WriteLine($"Name={name.Name} Version={name.Version}");
    }
}

This will exclude system assemblies and only show assemblies you’ve created:

Name=WeatherApp Version=1.2.0.0

Getting all loaded assemblies and outputting metadata as JSON

The following code collects the assembly name, version, location, build configuration, and target framework, then serializes it to JSON and outputs it:

using System.Reflection;
using System.Text.Json;
using System.Runtime.Versioning;

static void Main(string[] args)
{
    var jsonOptions = new JsonSerializerOptions() { IgnoreNullValues = true, WriteIndented = true };
    
    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
    {
        var metadataJson = JsonSerializer.Serialize(new
        {
            assembly.FullName,
            assembly.GetName().Name,
            version = assembly.GetName().Version.ToString(),
            assembly.Location,
            isMicrosoftAssembly = assembly.GetCustomAttribute<AssemblyCompanyAttribute>().Company == "Microsoft Corporation",
            buildConfig = assembly.GetCustomAttribute<AssemblyConfigurationAttribute>()?.Configuration,
            targetFramework = assembly.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName
        }, options: jsonOptions);

        Console.WriteLine(metadataJson);
    }
}

This will output JSON data like this:

{
  "FullName": "System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e",
  "Name": "System.Private.CoreLib",
  "version": "4.0.0.0",
  "Location": "C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App\\1.0\\System.Private.CoreLib.dll",
  "isMicrosoftAssembly": true,
  "buildConfig": "Release"
}
{
  "FullName": "WeatherApp, Version=1.2.0.0, Culture=neutral, PublicKeyToken=null",
  "Name": "WeatherApp",
  "version": "1.2.0.0",
  "Location": "D:\\Projects\\WeatherApp\\bin\\Debug\\net5.0\\WeatherApp.dll",
  "isMicrosoftAssembly": false,
  "buildConfig": "Debug",
  "targetFramework": ".NETCoreApp,Version=v5.0"
}