How to get root folder of ASP.NET application

前端 未结 4 2137
野趣味
野趣味 2021-01-03 09:00

I am trying to get the root folder of the application. I have used the following code, but this gives the bin folder, but what I need is the root folder of the

4条回答
  •  星月不相逢
    2021-01-03 09:37

    The location where your exe is, is the root of the application.

    You can use string appPath = Path.GetDirectoryName(Application.ExecutablePath); to get the application path.

    If you want to find the folder the solution is in, i suggest starting at the exe location, then walking up the directory tree until you get to a folder containing a .sln file. Not too sure why you'd like to do this though.

    EDIT: Just figured out you're creating an asp.net site. In which case you should be able to use below (found here):

    public static string MappedApplicationPath
    {
       get
       {
          string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
          if(APP_PATH == "/")      //a site
             APP_PATH = "/";
          else if(!APP_PATH.EndsWith(@"/")) //a virtual
             APP_PATH += @"/";
    
          string it = System.Web.HttpContext.Current.Server.MapPath(APP_PATH);
          if(!it.EndsWith(@"\"))
             it += @"\";
          return it;
       }
    }
    

提交回复
热议问题