代码之家  ›  专栏  ›  技术社区  ›  Maciej

如何获取应用程序使用的DLL的名称

  •  11
  • Maciej  · 技术社区  · 17 年前

    我正在寻找读取我的应用程序使用的所有程序集(.dlls)的方法。

    在标准的C项目中,有一个“引用”文件夹,当它被扩展时,我可以读取所有使用的库。

    我的目标是通过编程读取解决方案中每个项目使用的所有程序集。

    最后,我想看看我编译的*.exe应用程序使用了哪些库。

    6 回复  |  直到 10 年前
        1
  •  14
  •   Jon Skeet    17 年前

    你看过吗 Assembly.GetReferencedAssemblies ?

    请注意,您不使用的任何引用最终都不会被发送到元数据中,因此在执行时您不会看到它们。

    我已经用过 GetReferencedAssemblies 以前递归地查找命名类型而不必指定程序集。

        2
  •  9
  •   Marc Gravell    17 年前

    要正确执行此操作,需要遍历程序集,获取依赖项…如果您的exe需要dll_a,而dll_a需要dll_b(即使该exe没有引用它),那么您的exe也需要dll_b。

    您可以通过反射来查询这个(在任何程序集上);这需要一些工作(尤其是防止循环引用,因为 确实发生 ;下面是一个从“entry 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);
        }
    
        3
  •  3
  •   KV Prajapati    17 年前
    System.Reflection.Assembly []ar=AppDomain.CurrentDomain.GetAssemblies();
    
    foreach (System.Reflection.Assembly a in ar)
    {
     Console.WriteLine("{0}", a.FullName);
    }
    
        4
  •  1
  •   Dmytrii Nagirniak    17 年前

    你可以使用 AppDomain.GetAssemblies .
    但这将提供应用程序中显式或隐式使用的所有程序集。

        5
  •  0
  •   Matthew Scharley    17 年前

    如果你有 Assembly 对象,可以调用 GetReferencedAssemblies() 以获取程序集使用的任何引用。要获取当前运行的项目使用的程序集列表,可以使用:

    System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies()
    
        6
  •  0
  •   Warrior    17 年前

    我想你可以用:

    AssemblyName[] assemblies = this.GetType().Assembly.GetReferencedAssemblies();
    
    推荐文章