How to get Names of DLLs used by application

前端 未结 6 940
旧巷少年郎
旧巷少年郎 2021-01-11 09:26

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 lib

相关标签:
6条回答
  • 2021-01-11 09:51

    I guess you can use:

    AssemblyName[] assemblies = this.GetType().Assembly.GetReferencedAssemblies();
    
    0 讨论(0)
  • 2021-01-11 09:56

    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()
    
    0 讨论(0)
  • 2021-01-11 09:57

    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);
        }
    
    0 讨论(0)
  • 2021-01-11 10:01

    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.

    0 讨论(0)
  • 2021-01-11 10:07

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

    0 讨论(0)
  • 2021-01-11 10:10
    System.Reflection.Assembly []ar=AppDomain.CurrentDomain.GetAssemblies();
    
    foreach (System.Reflection.Assembly a in ar)
    {
     Console.WriteLine("{0}", a.FullName);
    }
    
    0 讨论(0)
提交回复
热议问题