asp.net 获取网站根目录

来源:互联网 发布:cf自动准备辅助源码 编辑:程序博客网 时间:2024/05/18 01:53

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

Server.MapPath(Request.ServerVariables["PATH_INFO"])
 Server.MapPath("/")
 Server.MapPath("")
Server.MapPath(".")
 Server.MapPath("../")
 Server.MapPath("..")
       Page.Request.ApplicationPath

以上的代码在http://localhost/EnglishClub/manage/WebForm1.aspx页面
运行结果:
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   这个才是获取的根目录,在写获取数据库路径是应该用这个,其他的都有问题。

 

转自:http://www.cnblogs.com/langge/archive/2009/10/23/1588715.html

原创粉丝点击