Problem
I see a few options for getting the application folder path:
What is the right course of action depending on the circumstances?
Asked by Leo Vo
Solution #1
AppDomain. CurrentDomain. For accessing files that are relative to the program install directory, BaseDirectory is probably the most beneficial.
This is the application root directory of an ASP.NET application, not the bin subfolder, which is presumably what you want. It will be the directory housing the primary executable of a client programme.
It will be the directory containing the VSTO managed assemblies for your programme in a VSTO 2005 application, not the path to the Excel executable, for example.
Others may return different folders depending on your setup – see @Vimvq1987’s response for an example.
CodeBase is the location where a file was discovered, and it can be a URL that starts with http://. In that situation, the assembly download cache will most likely be the location. For assemblies in the GAC, CodeBase is not guaranteed to be set.
UPDATE It’s recommended to use AppContext these days (.NET Core,.NET Standard 1.3+, or.NET Framework 4.6+). Instead of AppDomain, use BaseDirectory. CurrentDomain. BaseDirectory. Multiple AppDomains are no longer supported, but both are comparable.
Answered by Joe
Solution #2
will provide you the location of where the process was started – for example, “C:Program Files (x86)IIS Express” for a web app running in debug mode from Visual Studio.
will find out where you are. “file:C:hgServicesServicesServices.Websitebin” could be the dll that is running the code for a web app.
Now, in the case of a console app, points 2-6 will be the directory containing the.exe file.
I hope this has helped you save some time.
Answered by Matas Vaitkevicius
Solution #3
It’s worth noting that not all of these ways will produce the same result. They may yield the same value in some instances, but keep in mind that their functions are different:
Application.StartupPath
returns the StartupPath parameter (can be set when run the application)
System.IO.Directory.GetCurrentDirectory()
The current directory is returned, which may or may not be the same as the application’s location. The same may be said for the environment. CurrentDirectory. If you use this in a DLL file, it will return the path to the process’s current location (this is especially true in ASP.NET).
Answered by Quan Mai
Solution #4
To acquire the current web application root directory for a web application, normally call by web page for the current incoming request:
HttpContext.Current.Server.MapPath();
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
Above code description
Answered by Raj kumar
Solution #5
I started a process from a Windows Service over the Win32 API in the session from the user which is actually logged in (in Task Manager session 1 not 0). In this was we can get to know, which variable is the best.
The following are the outcomes for all 7 situations from the previous question:
Path1: C:\Program Files (x86)\MyProgram
Path2: C:\Program Files (x86)\MyProgram
Path3: C:\Program Files (x86)\MyProgram\
Path4: C:\Windows\system32
Path5: C:\Windows\system32
Path6: file:\C:\Program Files (x86)\MyProgram
Path7: C:\Program Files (x86)\MyProgram
Perhaps it will be useful to some of you who are looking for the ideal variable for your situation.
Answered by Beetee
Post is based on https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path