views:

292

answers:

6

I'm looking the way to read all assemblies (DLLs) used by my app.
In standard C# project there is "References" folder, when it is expanded I can read all libs used.

GOAL is read programically (in runtime) all assemblies used by each project in my solution.
Finally I'd like to see what libs are used by compiled *.exe application

Do you know the way??

+11  A: 

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.

Jon Skeet
+1  A: 

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

Dmytrii Nagirniak
As I understand it, that will give the ones which have already been loaded. There may be ones which are referenced in the metadata, but haven't been loaded yet as they haven't been needed in the execution so far.
Jon Skeet
Thanks for your tip! This AppDomain.CurrentDomain.GetAssemblies () was also interesting
Maciej
A: 

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()
Matthew Scharley
A: 

I guess you can use:

AssemblyName[] assemblies = this.GetType().Assembly.GetReferencedAssemblies();
Fossmo
+2  A: 
System.Reflection.Assembly []ar=AppDomain.CurrentDomain.GetAssemblies();

foreach (System.Reflection.Assembly a in ar)
{
 Console.WriteLine("{0}", a.FullName);
}
adatapost
+6  A: 

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);
    }
Marc Gravell
Will this also detect unmanaged dll's that are used via PInvoke, or does it only pick up manages dll's?
Matt Warren
Managed dlls only. You could use reflection to look for the P/Invoke methods - but it would be slightly different reflection.
Marc Gravell
How would I do that? My knowledge of reflection is limited, can you point me in the right direction?
Matt Warren
Updated to show PInvoke dlls
Marc Gravell
Thanks for that Marc
Matt Warren