tags:

views:

221

answers:

5

How can I get a list of assemblies in a given location? I'm trying to reflect over the appdomain, and that's giving me everything that's loaded. I just want what's in the executing assembly's directory.

+2  A: 

To get the executing assembly path:

using System.IO;

String path= Path.GetDirectoryName(
 System.Reflection.Assembly.GetExecutingAssembly());

To get all assemblies:

 DirectoryInfo di= new DirectoryInfo(path);
 FileInfo[] fis = di.GetFiles("*.dll");
 foreach(FileInfo fis in fls )
    //do something
cgreeno
+1  A: 

You can get a list of files using the System.IO namespace. Look at the Directory class. You then need to determine if the file is an assembly or not. You could inspect the binary looking for the IL Metadata, but that is out of scope for this answer.

You could also try and just load each file for reflection only using System.Reflection.Assembly.ReflectionOnlyLoadFrom().

JoshBerke
+1  A: 

Only by loading them, and noting which ones fail to load. This will of course leave the ones that didn't fail loaded in the current app-domain.

Only .NET runtime will tell you what is a valid assembly (and assemblies don't have to have a ".exe" or ".dll" extension).

Richard
A: 

To get all the assemblies loaded in the current app domain, you can use the GetAssemblies method:

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
CMS
thanks, but that gets me the framework assemblies as well. I just want the local ones.
Steve
A: 

The solution provided by BtBh works to find all files with extension .dll. Ricards remarks that only .Net runtime can tell you what is valid assembly. This means that you would have to explicitly load every .dll file found to check whether it is a valid .Net assembly (it could just be a plain old Windows dll). As mentioned by Josh there is a static method Assembly.ReflectionOnlyLoadFrom that allows you to load assemblies for inspection via reflection.

Jeroen Huinink