Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

webforms

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.

like image 299
Eddie Avatar asked Mar 25 '10 17:03

Eddie


People also ask

How to find root directory of Web application?

Right-click the Web application you want more information about, such as SharePoint (80), and then click Properties. In the Default Web Site Properties window, click the Home Directory tab. The Local Path field in this tab shows the Web application root folder.

How do I get the root path in .NET core?

The Application Base (Root) path is available in the ContentRootPath property of the interfaces IHostingEnvironment (. Net Core 2.0) and IWebHostEnvironment (. Net Core 3.0) in ASP.Net Core. The IHostingEnvironment is an interface for .

What is the root of Windows file system?

In windows the root folder would be the desktop. Desktop->Computer->C:\folder1\folder2 with the IShellFolder Interface.

How do I change the root directory in IIS?

Right-click the directory that you want to mark as an application root, and then click Properties. On the Directory tab, in the Application Settings section, click Create. In the Application name text box, type the name of the application, and then click OK. The virtual directory is now an application root.


2 Answers

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

like image 129
SLaks Avatar answered Sep 18 '22 23:09

SLaks


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.

like image 20
Justin Grant Avatar answered Sep 20 '22 23:09

Justin Grant