Maciej
Maciej

Reputation: 10805

How to get Names of DLLs used by application

I'm looking the way to read all assemblies (.dlls) used by my app.

In a standard C# project there is "References" folder, when it is expanded I can read all libraries used.

My goal is programatically read all assemblies which are used by each project in my solution.

Finally I'd like to see what libraries are used by my compiled *.exe application.

Upvotes: 14

Views: 5626

Answers (6)

Marc Gravell
Marc Gravell

Reputation: 1062502

To do this properly, you need to walk the assemblies, picking up the dependencies... if your exe needs Dll_A, and Dll_A needs Dll_B (even if the exe doesn't reference it), then your exe also needs Dll_B.

You can query this (on any assembly) via reflection; it takes a little work (especially to guard against circular references, which do happen; here's an example that starts at the "entry assembly", but this could just as easily be any assembly:

    List<string> refs = new List<string>();
    Queue<AssemblyName> pending = new Queue<AssemblyName>();
    pending.Enqueue(Assembly.GetEntryAssembly().GetName());
    while(pending.Count > 0)
    {
        AssemblyName an = pending.Dequeue();
        string s = an.ToString();
        if(refs.Contains(s)) continue; // done already
        refs.Add(s);
        try
        {
            Assembly asm = Assembly.Load(an);
            if(asm != null)
            {
                foreach(AssemblyName sub in asm.GetReferencedAssemblies())
                {
                    pending.Enqueue(sub);
                }
                foreach (Type type in asm.GetTypes())
                {
                    foreach (MethodInfo method in type.GetMethods(
                        BindingFlags.Static | BindingFlags.Public |
                             BindingFlags.NonPublic))
                    {
                        DllImportAttribute attrib = (DllImportAttribute)
                            Attribute.GetCustomAttribute(method,
                                typeof(DllImportAttribute));
                        if (attrib != null && !refs.Contains(attrib.Value))
                        {
                            refs.Add(attrib.Value);
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.Error.WriteLine(ex.Message);
        }
    }
    refs.Sort();
    foreach (string name in refs)
    {
        Console.WriteLine(name);
    }

Upvotes: 11

KV Prajapati
KV Prajapati

Reputation: 94625

System.Reflection.Assembly []ar=AppDomain.CurrentDomain.GetAssemblies();

foreach (System.Reflection.Assembly a in ar)
{
 Console.WriteLine("{0}", a.FullName);
}

Upvotes: 2

Fossmo
Fossmo

Reputation: 2892

I guess you can use:

AssemblyName[] assemblies = this.GetType().Assembly.GetReferencedAssemblies();

Upvotes: 0

Matthew Scharley
Matthew Scharley

Reputation: 132234

If you have an Assembly object, you can call GetReferencedAssemblies() on it to get any references that assembly uses. To get a list of assemblies the currently running project uses, you can use:

System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies()

Upvotes: 0

Dmytrii Nagirniak
Dmytrii Nagirniak

Reputation: 24078

You can use AppDomain.GetAssemblies.
But this will give ALL assemblies used explicitly or implicitly in your application.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499770

Have you looked at Assembly.GetReferencedAssemblies?

Note that any references you don't use won't end up being emitted into the metadata, so you won't see them at execution time.

I've used GetReferencedAssemblies recursively before now to find a named type without having to specify the assembly.

Upvotes: 14

Related Questions