怎样获得网站的根目录

if I understand you correctly, you can either

using System.DirectoryServices;

 DirectoryEntry de 
= new DirectoryEntry("IIS://LOCALHOST/W3SVC/1/ROOT");
   
string s = (string)de.Properties["Path"].Value;
   Response.Write(s 
+ "<BR>");

or

   s 
= System.Web.HttpContext.Current.Server.MapPath("/"); 

or simply 

  s
= Server.MapPath("/");
  Response.Write(s 
+ "<BR>");

IIS 的当前网站路径:Server.MapPath("/"
虚拟目录路径: Application(
"Path"= Server.MapPath("."+ "\" '
          or   Request.ServerVariables("APPL_PHYSICAL_PATH")

Server.MapPath("/") 站点根目录
Server.MapPath(
".")  文件目录

也许楼主要的是网站逻辑路径的根目录:

' Request.ApplicatonPath 仅适用于开发调试的 localhost ,并不等于网站的路径
'
 由于网站可能具有多个 IP 地址,一是内部 IP 地址,对外由 NAT 转换为外网地址
'
 因此对于不同的访问者,AppPath 并不完全相同
'
 AppPath 必须存放在 Session 中,而不能存放在 Application 全局变量中

Dim UrlAuthority As String 
=
    HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)
If HttpContext.Current.Request.ApplicationPath 
= "/" Then
    
' 直接安装在 Web 站点
    HttpContext.Current.Session("AppPath"= UrlAuthority
Else
    
' 安装在虚拟子目录下
    HttpContext.Current.Session("AppPath"= UrlAuthority & 
        HttpContext.Current.Request.ApplicationPath
End If

urlSuffix = Context.Request.Url.Host & Context.Request.ApplicationPath
posted @ 2004-11-14 02:31  Benny Ng  阅读(2154)  评论(0编辑  收藏  举报