Find out dependencies of all DLLs?

前端 未结 6 1187
北恋
北恋 2020-12-28 12:57

I have a collection of DLLs(say 20). How do I find out all the DLLs on which one specific DLL (say DLL A) is depending upon?

6条回答
  •  执念已碎
    2020-12-28 13:47

    If you want the DLL's (the files) then, Assembly.GetReferencedAssemblies will also return the .Net Framework assemblies.

    Here is a simple code snippet that will get the dll's it can find in the current directory (and also include some other related files):

    private readonly string[] _extensions = { ".dll", ".exe", ".pdb", ".dll.config", ".exe.config" };
    
    private string[] GetDependentFiles(Assembly assembly)
    {
        AssemblyName[] asm = assembly.GetReferencedAssemblies();
        List paths = new List(asm.Length);
        for (int t = asm.Length - 1; t >= 0; t--)
        {
            for (int e = _extensions.Length - 1; e >= 0; e--)
            {
                string path = Path.GetFullPath(asm[t].Name + _extensions[e]);
                if (File.Exists(path)) paths.Add(path);
            }
        }
    
        return paths.ToArray();
    }
    

    You can call it like so: MessageBox.Show(string.Join("\r\n", GetDependentFiles(Assembly.GetEntryAssembly())));

提交回复
热议问题