Home > Article > Backend Development > Summary of how .NET obtains the current path
This article summarizes various methods for obtaining the current path in .NET (including ASP.NET/WinForm, etc.), which has certain reference value. Let’s take a look at it with the editor.
The following is summarized. NET (including ASP.NET/WinForm, etc.) various methods to obtain the current path
//Get the full path of the current process, including the file name (process name).
string str = this.GetType().Assembly.Location;
result: X:\xxx\xxx\xxx.exe (directory where the .exe file is located + .exe file name)
//Get the new Process component and associate it with the full path of the main module of the currently active process, including the file name (process name).
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (directory where the .exe file is located + .exe file name)
//Gets and sets the fully qualified path of the current directory (i.e. the directory from which the process was started).
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (the directory where the .exe file is located)
//Get the base directory of the current application domain of the current Thread , which is used by the assembly conflict resolver to probe assemblies.
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (the directory where the .exe file is located + "\")
//Getting and setting include The name of the application's directory.
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (the directory where the .exe file is located + "\")
//Get startup The path to the application's executable file, excluding the executable file name.
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (the directory where the .exe file is located)
//Get the path to start the application The path to the executable file, including the name of the executable file.
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (the directory where the .exe file is located + the name of the .exe file)
//Get the current working directory of the application (unreliable).
string str = System.IO.Directory.GetCurrentDirectory();
result: X:\xxx\xxx (the directory where the .exe file is located)
//Get the full path of the current process, Contains the file name (process name).
string str = this.GetType().Assembly.Location;
result: X:\xxx\xxx\xxx.exe (directory where the .exe file is located + .exe file name)
//Get the new Process component and associate it with the full path of the main module of the currently active process, including the file name (process name).
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (directory where the .exe file is located + .exe file name)
//Gets and sets the fully qualified path of the current directory (i.e. the directory from which the process was started).
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (the directory where the .exe file is located)
//Get the base directory of the current application domain of the current Thread , which is used by the assembly conflict resolver to probe assemblies.
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (the directory where the .exe file is located+"\")
//Getting and setting include The name of the application's directory.
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (the directory where the .exe file is located+"\")
//Get startup The path to the application's executable file, excluding the executable file name.
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (the directory where the .exe file is located)
//Get the address of the started application The path to the executable file, including the name of the executable file.
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (directory where the .exe file is located + .exe file name)
//Get the current working directory of the application (unreliable).
string str = System.IO.Directory.GetCurrentDirectory();
result: X:\xxx\xxx (the directory where the .exe file is located)
Three ways to get the current path in .NET Code
//Web Programming
HttpContext.Current.Server.MapPath("FileName")
System.Web.HttpContext.Current.Request.Path
//Windows Programming
System.Environment.CurrentDirectory
//Mobile Programming
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
The above is the detailed content of Summary of how .NET obtains the current path. For more information, please follow other related articles on the PHP Chinese website!