Only load managed-load DLLs into the directory

I need to load all assemblies from a DLL into a directory.

My base code is:

var assemblies = from filename in Directory.GetFiles(HttpRuntime.BinDirectory, "*.dll")
                 select Assembly.LoadFrom(filename);

However, if there are unmanaged DLLs in this directory, then Assembly.LoadFrom fails. Is there a way to load only managed DLLs? Capturing a boot exception is an option, but I would like to know if there is a better way.

My code runs on ASP.NET when the application starts. Therefore, I also agree with the specific ASP.NET solution.

+5
source share
4 answers

from here: http://blogs.msdn.com/b/junfeng/archive/2004/02/06/68334.aspx

"Assembly.LoadFrom will throw a BadImageFormatException if this file is not a managed assembly.

. . BadImageFormatException, HResult. HResult COR_E_ASSEMBLYEXPECTED, , . "

?

+4

Assembly.LoadFrom try...catch.

+5

: , - . , .

DLL, , , ( , ). , try...catch, , , .

+3
source

Call GetAssemblyName(), and if it throws a BadImageFormatException, then this is not an assembly.

+1
source

All Articles