tags:

views:

70

answers:

3

Similar to Ubiquitous way to get the root directory an application is running in via C#, but that question seems to be related to Win Forms. How would the same be done for Web Forms?

I was using...

HttpContext.Current.Server.MapPath("~")

This works great for handling HTTP requests but seems not to work if a scheduler like Quartz.NET invokes a job (the problem I am having). The HttpContext.Current is null in that scenario since an actual HTTP request is not made.

+4  A: 

Try the System.Web.Hosting.HostingEnvironment.MapPath method. AFAIK it's not dependent on HttpContext so you should be able to use it from a background thread.

Justin Grant
+1 System.Web.Hosting.HostingEnvironment.MapPath("~") worked for returning the web application root.
Eddie
+4  A: 

You're looking for the HttpRuntime.AppDomainAppPath property, and perhaps the VirtualPathUtility class.

SLaks
HttpRuntime.AppDomainAppPath worked for returning the web application root. This seems to be the more straight forward property for obtaining root.
Eddie
A: 

Use AppDomain.CurrentDomain.BaseDirectory. This corresponds to AppDomainSetup.ApplicationBase which states that it is 'The name of the application base directory.'

http://msdn.microsoft.com/en-us/library/system.appdomain.basedirectory.aspx

http://msdn.microsoft.com/en-us/library/system.appdomainsetup.applicationbase.aspx

Sean Kinsey