Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to determine application root directory?

Tags:

c#

.net

winforms

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?

like image 327
abatishchev Avatar asked Dec 12 '08 13:12

abatishchev


People also ask

How do I find application root directory?

Right-click the Web application you want more information about, such as SharePoint (80), and then click Properties. In the Default Web Site Properties window, click the Home Directory tab. The Local Path field in this tab shows the Web application root folder.

What is application root directory?

The root directory contains all other folders in the drive or folder, and can, of course, also contain files. You can visualize this with an upside-down tree where the roots (the root folder) are at the top and the branches (subfolders) fall below; the root is what holds together all of its lower items.

How do I find the root directory in node?

If you want the root directory of the running process, you probably do want to use process. cwd() . If you want predictability and reliability, then you probably need to make it a requirement of your application that a certain environment variable is set.

How do I find the root directory of my website?

Usually you go to http://example.com and it shows you the file "index. html". But for example, oftentimes if there is a file at http://example.com/images/picture.jpg then you could go to http://example.com/images/ and the site would show you the directory /images/ and all the files inside.


2 Answers

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.

like image 52
Greg Dean Avatar answered Sep 30 '22 01:09

Greg Dean


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; } 
like image 33
Rob Prouse Avatar answered Sep 30 '22 03:09

Rob Prouse