Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 943
Maciej Avatar asked Aug 20 '09 09:08

Maciej


People also ask

How can I see the details of a DLL?

To view DLL propertiesSelect the desired DLL and right-click the file. Click Properties and Version.

How do I view DLLs in Process Explorer?

Running Process ExplorerOn the View menu, make sure Show Lower Pane is checked. Press CTRL+D or select View > Lower Pane View > DLLs to enable DLL view mode.


2 Answers

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.

like image 155
Jon Skeet Avatar answered Nov 09 '22 02:11

Jon Skeet


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);
    }
like image 22
Marc Gravell Avatar answered Nov 09 '22 02:11

Marc Gravell