开发者

Ubiquitous way to get the file system root of a .NET WEB application

开发者 https://www.devze.com 2022-12-24 12:14 出处:网络
Similar to Ubiquitous way to get the root directory an application is running in via C#, but that question seems to be related to W开发者_开发知识库in Forms. How would the same be done for Web Forms?

Similar to Ubiquitous way to get the root directory an application is running in via C#, but that question seems to be related to W开发者_开发知识库in 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.


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消