Determine the list of files which DLL uses in C# p

2019-04-16 23:25发布

问题:

I will try to describe my question... I load, for example, "General.dll" in my project (C#) using

//load "General.dll"
dllHandle = LoadLibraryEx(generalPath, IntPtr.Zero, LOAD_WITH_ALTERED_SEARCH_PATH);

[DllImport("kernel32.dll")]         
private static extern IntPtr LoadLibraryEx(string dllToLoad, IntPtr reserved, uint flags);

[DllImport("General.dll")] ...

If I use a method from "General.dll" which requires "Gif.dll" I need to copy "Gif.dll" to the folder with "General.dll" (if I do not use such method I can do not copy "Gif.dll").

If I use a method from"General.dll" which uses "Dutch" language, I need to copy "Dutch.amd" to the folder with "General.dll".

For example, I have wrote some code which uses different methods from "General.dll". How to determine which files from the folder with "General.dll" I can delete?

Can I do this using Visual Studio? For example, compile my code in VS, find out which files were used during runtime and copy only these files.

Thanks!

回答1:

Typically, any redistributable component will include deployment guidelines which state what files must be deployed, where they must be deployed to, and any registration that must occur. I highly recommend you review the documentation for general.dll, if it exists.

If general.dll is your own work, then look at the code to find all referenced files and assemblies. If it is too voluminous, a decent heuristic might be to run Process Monitor while using the component to monitor file and registry access. Many installer authoring tools use similar techniques to detect dependencies.

If you do not have documentation nor source code for the component in question, you can use dumpbin /dependents, Dependency Walker / depends.exe, or even just strings to find the imports declared. Beware, however, that this list is not exclusive, and may leave out critical files loaded conditionally (only on certain machine types, cultures, or otherwise).



回答2:

Maybe DependencyWalker is the tool you are looking for. Just open an EXE or DLL file with this tool and it builds up a nice tree of dependencies to DLLs that are required. It also supports profiling to detect dynamic dependencies while runtime and offers many other features.