asp.net 获取网站根目录

获取网站根目录的方法有几种如:

Server.MapPath(Request.ServerVariables["PATH_INFO"])

Server.MapPath("/")

Server.MapPath("")

Server.MapPath(".")

Server.MapPath("../")

Server.MapPath("..")

Page.Request.ApplicationPath

运行结果:

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

以上的方法可以在.aspx中访问,但是如果你在。cs文件就不能用。

HttpContext.Current.Server.MapPath();

System.Web.HttpContext.Current.Request.PhysicalApplicationPath

在.cs文件中可以用。

但是HttpContext.Current.Server.MapPath();这个获取的是文件的路径而不是根目录。

只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath 这个才是获取的根目录,在写获取数据库路径是应该用这个,其他的都有问题。