There are several ways to get the root directory of a Web site, such as:
Server.MapPath (request.servervariables["Path_info"]) Server.MapPath ("/") Server.MapPath ("")//the directory in which the current code file is located road strengthServer.MapPath (".") Server.MapPath (".. /") Server.MapPath ("..")
The above code is on the http://localhost/EnglishClub/manage/WebForm1.aspx page
Operation Result:
C:\Inetpub\wwwroot\EnglishClub\manage\WebForm1.aspx
C:\Inetpub\wwwroot\
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\manage
C:\Inetpub\wwwroot\EnglishClub\
C:\Inetpub\wwwroot\EnglishClub
The above methods can be accessed in. aspx, but if you are in. CS file is not available.
HttpContext.Current.Server.MapPath ();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath can be used in. cs files. But HttpContext.Current.Server.MapPath (); This gets the path to the file rather than the root directory.
Only System.Web.HttpContext.Current.Request.PhysicalApplicationPath this is the root directory to get, in the write to get the database path is should use this, the others have problems.
System.Web.HttpContext.Current.Request.PhysicalApplicationPath
and Server.MapPath ("~/") effect is the same.
Server.MapPath ("~/");//Regardless of the file in which the code resides, what the page path is, always return to C:\Inetpub\wwwroot\EnglishClub\ (the root directory where the current program is running)
The absolute path should not be saved if the path to the storage attachment is entered into the database. Only the file name section should be stored. For example:
/uploads/abc.txt
When you need to browse the file, in the path read out: (that is,/uploads/abc.txt), front + Web site of the road: for example:
http://abc.com+ "/uploads/abc.txt"
Transferred from: http://www.cnblogs.com/wenbo/archive/2011/08/09/2132620.html
. NET ways to get the Web site root directory