c # console application to obtain root directory

1, to obtain the root of the console application method

The method of 1, Environment.CurrentDirectory fully qualified path to get or set the current working directory of the
method 2, AppDomain.CurrentDomain.BaseDirectory acquired directory group, which is solved by the assembly for detecting assembly procedure conflict

// Get path using the current execution of the application set parent directory
dir = Path.GetFullPath ( "..");
// Get path using higher current application assemblies perform directory of the parent directory
dir = Path.GetFullPath ( " ../ .. ");

2, to obtain the root directory of the Web application method

The method of 1, HttpRuntime.AppDomainAppPath.ToString (); // Get carried physical drive path of the application directory application's current application domain. App_Data for obtaining
Method 2, Server.MapPath ( "") or Server.MapPath ( "~ /"); // return to the physical file path specified virtual path on a Web server opposing
method 3, Request.ApplicationPath; // get the root directory on the virtual application server ASP.NET application

3, to obtain the root directory of the application of the method WinForm

1, Environment.CurrentDirectory.ToString (); // get or set the current working directory of the fully qualified path
2, Application.StartupPath.ToString (); // get the path to start the application's executable file, without the executable name of the file
3, Directory.GetCurrentDirectory (); // get the application's current working directory
4, AppDomain.CurrentDomain.BaseDirectory; // get base directory, it resolved by the assembly procedure used to detect conflicts assembly
5, AppDomain. CurrentDomain.SetupInformation.ApplicationBase; // Gets or sets the name of the directory that contains the application

Where: The following are two ways to obtain the executable file name
1, Process.GetCurrentProcess () MainModule.FileName; // obtain the file name of the currently executing exe.
2, Application.ExecutablePath; // get the path to start the application's executable file, including the name of the executable file

Guess you like

Origin www.cnblogs.com/superfeeling/p/12076193.html