64

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?

5 Answers 5

89

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.

3
  • 10
    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
    Commented Dec 12, 2008 at 15:52
  • I have had to downvote as in .NET 8 at least "AppDomain.CurrentDomain.BaseDirectory" will often return a temporary AppData folder and not the EXE folder. So BaseDirectory is not a consistent way to get exe root.
    – viion
    Commented Dec 22, 2023 at 0:15
  • What do you mean by often? What does that depend on. At any case thanks for pointing out the potential change
    – Greg Dean
    Commented Dec 23, 2023 at 2:40
26

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;
}
3
  • 36
    I especially like determining my ass.Location ;D
    – Rob Prouse
    Commented Dec 12, 2008 at 16:25
  • 2
    I pretend to think that the variable ass stands for the Assembly word Lol. Commented Dec 9, 2016 at 9:07
  • I guess the joke must have been on an aeroplane on December 9th, 2016
    – jamheadart
    Commented Jul 16, 2019 at 17:08
8

This is an old question but I always used to use:

Environment.CurrentDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);

However, looking at the solutions in here I think some simple tests need to be done:

var r = new List<long>();
var s = Stopwatch.StartNew();

s.Restart();
string root1 = Application.StartupPath;
r.Add(s.ElapsedTicks);

s.Restart();
string root2 = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
r.Add(s.ElapsedTicks);

s.Restart();
string root3 = Path.GetDirectoryName(new FileInfo(Assembly.GetExecutingAssembly().Location).FullName);
r.Add(s.ElapsedTicks);

s.Restart();
string root4 = AppDomain.CurrentDomain.BaseDirectory;
r.Add(s.ElapsedTicks);

s.Restart();
string root5 = Path.GetDirectoryName(Assembly.GetAssembly( typeof( Form1 ) ).Location);
r.Add(s.ElapsedTicks);

The results in ticks:

  • 49
  • 306
  • 166
  • 26
  • 201

So it seems AppDomain.CurrentDomain.BaseDirectory is the way to go.

2

If you want to get the application root folder path use below code sample.

string path =new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName
4
  • 1
    I don't think this is what OP meant by the root directory, although it was five years ago so it's hard to know for sure.
    – Rawling
    Commented Aug 5, 2013 at 11:15
  • I don't know if that answer is related to the ask, but it solve my problem.
    – Marcelo
    Commented Nov 28, 2017 at 7:11
  • 1
    In development env. it will work fine but in production, it wont work.
    – Bimal Das
    Commented Feb 15, 2019 at 6:58
  • System.Environment.CurrentDirectory, just in case if other people which library it's derived from. Commented Jun 10, 2019 at 13:59
0

I use

Path.GetDirectoryName(new Uri(Assembly.GetEntryAssembly().CodeBase).LocalPath)

The Assembly.Location will point to the shadow copy if you use shadow copying, so using CodeBase is a better option, but CodeBase is a Url.

Not the answer you're looking for? Browse other questions tagged or ask your own question.