Best way to get application folder path
Note that not all of these methods will return the same value. In some cases, they can return the same value, but be careful, their purposes are different:
Application.StartupPath
returns the StartupPath
parameter (can be set when run the application)
System.IO.Directory.GetCurrentDirectory()
returns the current directory, which may or may not be the folder where the application is located. The same goes for Environment.CurrentDirectory
. In case you are using this in a DLL file, it will return the path of where the process is running (this is especially true in ASP.NET).
For a web application, to get the current web application root directory, generally call by web page for the current incoming request:
HttpContext.Current.Server.MapPath();
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
Above code description
Application.StartupPath
and 7.System.IO.Path.GetDirectoryName(Application.ExecutablePath)
- Is only going to work for Windows Forms applicationSystem.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().Location)
Is going to give you something like:
"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\legal-services\\e84f415e\\96c98009\\assembly\\dl3\\42aaba80\\bcf9fd83_4b63d101"
which is where the page that you are running is.AppDomain.CurrentDomain.BaseDirectory
for web application could be useful and will return something like"C:\\hg\\Services\\Services\\Services.Website\\"
which is base directory and is quite useful.System.IO.Directory.GetCurrentDirectory()
and 5.Environment.CurrentDirectory
will get you location of where the process got fired from - so for web app running in debug mode from Visual Studio something like "C:\\Program Files (x86)\\IIS Express"
System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
will get you location where .dll
that is running the code is, for web app that could be "file:\\C:\\hg\\Services\\Services\\Services.Website\\bin"
Now in case of for example console app points 2-6 will be directory where .exe
file is.
Hope this saves you some time.
AppDomain.CurrentDomain.BaseDirectory
is probably the most useful for accessing files whose location is relative to the application install directory.
In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is probably what you usually want. In a client application, it will be the directory containing the main executable.
In a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your application, not, say, the path to the Excel executable.
The others may return different directories depending on your environment - for example see @Vimvq1987's answer.
CodeBase
is the place where a file was found and can be a URL beginning with http://. In which case Location
will probably be the assembly download cache. CodeBase is not guaranteed to be set for assemblies in the GAC.
UPDATE
These days (.NET Core, .NET Standard 1.3+ or .NET Framework 4.6+) it's better to use AppContext.BaseDirectory
rather than AppDomain.CurrentDomain.BaseDirectory
. Both are equivalent, but multiple AppDomains are no longer supported.