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?
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.
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.
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.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With