What is the best way to determine the root directory of an application?

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

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 any better ways?

+41
c # winforms
Dec 12 '08 at 13:49
source share
5 answers

AppDomain.CurrentDomain.BaseDirectory is my way to this.

However:

Application.StartupPath gets the directory of your executable

AppDomain.BaseDirectory gets the directory used to resolve assemblies

Since they may be different, you might want to use Application.StartupPath unless you care about building permissions.

+45
Dec 12 '08 at 14:04
source share

It depends. If you want the EXE directory to run the application, then either of your two examples will work. Remember, however, that .NET is very flexible, and it may be that another application is associated with your EXE and calls it, possibly from a different directory.

This does not happen very often, and you would probably write if that were the case, but it is an opportunity. Because of this, I prefer to specify which assembly I am interested in and get a catalog from it. Then I know that I get all the DLLs in the same directory as the specific assembly. For example, if you have a MyApp.exe application with the class in it MyApp.MyClass, then you will do this:

 string root = string.Empty; Assembly ass = Assembly.GetAssembly( typeof( MyApp.MyClass ) ); if ( ass != null ) { root = ass.Location; } 
+18
Dec 12 '08 at 14:10
source share

This is an old question, but I always used:

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

However, looking at the solutions here, I think we need to do some simple tests:

 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); 

Tick ​​results:

  • 49
  • 306
  • 166
  • 26
  • 201

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

+4
Sep 02 '14 at 17:29
source share

I use

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

Assembly.Location will point to a shadow copy if you are using shadow copy, so it is better to use CodeBase, but CodeBase is Url.

0
Aug 22 '17 at 17:26
source share

If you want the root path of the application to be used below the sample code.

 string path =new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.FullName 
-one
Aug 05 '13 at 10:52
source share



All Articles