This article summarizes the ASP.net programming to obtain the project root directory implementation method. Share to everyone for your reference, specific as follows:
When writing a program, you often need to use the project root directory. Summarize the following yourself
1, get the console application root directory method
Method 1, Environment.currentdirectory Gets or sets the full qualified path of the current working directory
Method 2, AppDomain.CurrentDomain.BaseDirectory gets the base directory, which is used by the Assembly resolver to probe assemblies
2, get the Web application root directory method
Method 1, HttpRuntime.AppDomainAppPath.ToString ();//Get the physical drive path of the application directory that hosts the application in the current application domain. Used in App_Data to get
Method 2, Server.MapPath (""), or Server.MapPath ("~/");//returns the physical file path relative to the specified virtual path on the Web server
Method 3, request.applicationpath;//get the virtual application root directory of the ASP.net application on the server
3, obtain the WinForm application root directory method
①environment.currentdirectory.tostring ()//Get or set the fully qualified path of the current working directory
②application.startuppath.tostring ()//Gets the path to the executable that started the application, excluding the name of the executable file
③directory.getcurrentdirectory ()//Get the current working directory of the application
④appdomain.currentdomain.basedirectory;//gets the base directory, which is used by the Assembly resolver to probe assemblies
⑤appdomain.currentdomain.setupinformation.applicationbase;//Gets or sets the name of the directory that contains the application
Where: The following two methods can get the name of the execution file
1, Process.getcurrentprocess (). mainmodule.filename;//can get the file name of the currently executing EXE.
2. application.executablepath;//gets the path to the executable file that initiated the application, including the name of the executable file
I believe a lot of friends who use Asp.net+access website often have such a requirement: to get the physical path of an Access database in the database Access Layer class library. And then splicing the database connection string for database-related operations. At the site UI layer there are a number of ways to get a physical path to a Web site, such as:
1. Request.physicalapplicationpath
2. Request.mappath ("~/"), but not with these methods at the database access layer
Using System.Reflection;
Using System.IO; Don't forget to refer to these two namespaces before using
///<summary>
///Get the physical path to the Access database
///</summary>
///<returns ></returns> public
static string Getdbpath ()
{
string str = assembly.getexecutingassembly (). Location;
str = path.getdirectoryname (str) + @ "\__assemblyinfo__.ini";
str = File.readalltext (str, System.Text.Encoding.Unicode);
int index = str. IndexOf ("file:///") + 8;
int length = str. IndexOf ("/bin");
str = str. Substring (index, length-index);
str = str. Replace ('/', ' \ n ');
str = @ "\app_data\db.mdb";
return str; The final return is the physical path to the database.
}
Code Explanation:
1. String str = assembly.getexecutingassembly (). Location;
The value obtained is a temporary directory, such as: "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary asp.net files\myproject\ba81bed7\ A7082081\assembly\dl3\62f82680\8345eb5b_37a6c901\abc.dll
2.STR = Path.getdirectoryname (str) + @ "\__assemblyinfo__.ini";
The key is this sentence, in the DLL file under the same directory there is a "__assemblyinfo__.ini" file, with Notepad open can find the actual physical address contains a DLL, know this is good to do.
3.
str = File.readalltext (str, System.Text.Encoding.Unicode);
int index = str. IndexOf ("file:///") + 8;
int length = str. IndexOf ("/bin");
str = str. Substring (index, length-index);
str = str. Replace ('/', ' \ n ');
str = @ "\app_data\cms.mdb";
The code is not explained in detail, that is, the INI file read out, find out the actual Site Bin folder physical path, and then splicing the database file name, you get the physical path of the database, but this method effective premise is the database file and the site in the same directory.
I hope this article will help you to ASP.net program design.