ASP.NET - How to get the physical application path

Inside of a page request, you can use HttpContext.Current.Request.PhysicalApplicationPath:

  protected void Page_Load(object sender, EventArgs e)
  {
      string path = HttpContext.Current.PhysicalApplicationPath;
      // C:\inetpub\wwwroot\
 
      // Which is the same as:
      HttpContext context = HttpContext.Current;
      HttpRequest request = context.Request;
      string path = request.PhysicalApplicationPath;
  }
  

If you aren't processing a page request, you can use HttpRuntime.AppDomainAppPath.


  protected void Application_Start(object sender, EventArgs e)
  {
      string path = HttpRuntime.AppDomainAppPath;
      // C:\inetpub\wwwroot
  }
  

If you attempt to access HttpContext.Current.Request in the above example, ASP.NET will throw an exception: "Request is not available in this context"


Ads by Google


Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath