C#.Net獲取網(wǎng)站根目錄地址


      /// <summary>
      /// C#取得網(wǎng)站的根目錄的URL
      /// </summary>
      /// <returns></returns>
      public static string GetRootURI()
      {
          string AppPath = "";
          HttpContext HttpCurrent = HttpContext.Current;
          HttpRequest Req;
          if (HttpCurrent != null)
          {
              Req = HttpCurrent.Request;
  
              string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);
              if (Req.ApplicationPath == null || Req.ApplicationPath == "/")
                  //直接安裝在   Web   站點   
                  AppPath = UrlAuthority;
              else
                  //安裝在虛擬子目錄下   
                  AppPath = UrlAuthority   Req.ApplicationPath;
          }
          return AppPath;
      }




C#.Ne獲取網(wǎng)站根目錄的物理路徑 


public static string GetRootPath()
        {
            string AppPath = "";
            HttpContext HttpCurrent = HttpContext.Current;
            if (HttpCurrent != null)
            {
                AppPath = HttpCurrent.Server.MapPath("~");
            }
            else
            {
                AppPath = AppDomain.CurrentDomain.BaseDirectory;
                if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
                    AppPath = AppPath.Substring(0, AppPath.Length - 1);
            }
            return AppPath;
        }


原文鏈接:C#獲取網(wǎng)站根目錄URL和網(wǎng)站根目錄的物理路徑