views:

1041

answers:

3

I need to get all dlls in my application root directory. What is the best way to do that?

string root = Application.StartupPath;

or

string root = new FileInfo(Assembly.GetExecutingAssembly().Location).FullName;

and after that

Directory.GetFiles(root, "*.dll");

Which way is better? Are there better ways?

+4  A: 

AppDomain.CurrentDomain.BaseDirectory is my go to way of doing so.

However:

Application.StartupPath gets the directory of your executable

AppDomain.BaseDirectory gets the directory used to resolve assemblies

Since they can be different, perhaps you want to use Application.StartupPath, unless you care about assembly resolution.

Greg Dean
Correct, I follow the same path ;-)
BeowulfOF
Application.StartupPath will be effected by "Working Directory" if it's set in the exe shortcut, or the process is started from another apps. I didn't realized it until much later, which had cost me days of sleepless night to troubleshoot what went wrong.
faulty
+9  A: 

It depends. If you want the directory of the EXE that started the application, then either of your two examples will work. Remember though, that .NET is very flexible, and it could be that another application has linked to your EXE and is calling it, possibly from another directory.

That doesn't happen very often and you would probably have written if it did, but it is a possibility. Because of that, I prefer to specify which assembly I am interested in and get the directory from that. Then I know that I am getting all of the DLLs in the same directory as that specific assembly. For example, if you have an application MyApp.exe with a class in it MyApp.MyClass, then you would do this;

string root = string.Empty;
Assembly ass = Assembly.GetAssembly( typeof( MyApp.MyClass ) );
if ( ass != null )
{
   root = ass.Location;
}
Rob Prouse
Nice variable naming
Bob
I especially like determining my ass.Location ;D
Rob Prouse
It requires two hands.
Steven Sudit
A: 
Environment.CurrentDirectory
abatishchev