获得网站根目录路径

  //保存文件路径供DMT调用
         string CreatePath = "";
         public string  PathTy(int sealerID, int Type,string List)
         {
             CreateFolder("temp");
             string PathT =CreatePath+"\\"+ sealerID + "_" + Type + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";

             FileStream file = new FileStream(PathT, FileMode.Create, FileAccess.Write);
                 StreamWriter sw = new StreamWriter(file);
                 sw.Write(List);
                 sw.Close();

                 string path = GetRootURI() + "/" + "temp" + "/" + sealerID + "_" + Type + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                 return path;
         }
      

public 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;
             }
           //  CreateFolder("temp");
             return AppPath;
         }
        //创建文件夹
         public string CreateFolder(string FolderPathName)
         {
             if (FolderPathName.Trim().Length > 0)
             {
                  CreatePath   = System.Web.HttpContext.Current.Server.MapPath( FolderPathName).ToString();
                     if (!Directory.Exists(CreatePath))
                     {
                         Directory.CreateDirectory(CreatePath);
                     }
             }
             return CreatePath;
         }

原文地址:https://www.cnblogs.com/jimmychow/p/2307891.html