127

So I have a web project, and I'm trying to get the root directory of the website using the c# method Directory.GetCurrentDirectory(). I don't want to be using a static path as the file locations will be changing in the future. This method is running in my imageProcess.aspx.cs file, but where I thought it would return:

C:\Users\tcbl\documents\visual studio 2010\Projects\ModelMonitoring\ModelMonitoring\imageProcess.aspx.cs

I'm instead getting:

C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\

Can anyone explain why this is happening and what a possible solution might be? Thanks a lot.

Bali C
  • 30,582
  • 35
  • 123
  • 152
Julian Coltea
  • 3,599
  • 10
  • 26
  • 32
  • Related post [here](https://stackoverflow.com/q/6041332/465053) which talks about .Net application execution paths in general. – RBT Nov 14 '17 at 10:33
  • Another post which talks about [server map paths in web applications in .Net](https://stackoverflow.com/q/275781/465053) – RBT Nov 14 '17 at 10:34

3 Answers3

235

The current directory is a system-level feature; it returns the directory that the server was launched from. It has nothing to do with the website.

You want HttpRuntime.AppDomainAppPath.

If you're in an HTTP request, you can also call Server.MapPath("~/Whatever").

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 5
    Thanks. I was actually looking for `HttpRuntime.BinDirectory`, but that was easy to determine in the debugger, once I tried what you mentioned in your answer. – Kent Weigel Nov 30 '16 at 01:11
  • If I use `Server.MapPath("~Whatever") and the IIS site is hosted in http://myserver. I get a C:\\somefolder\Whatever, rather than http://myserver/Whatever folder. – Si8 Mar 30 '17 at 19:30
  • @Si8: Yes; that's what `Server.MapPath` does. You want http://stackoverflow.com/q/5823847/34397 – SLaks Apr 06 '17 at 17:02
  • 2
    In case you are unfamiliar with .NET assemblies (or are in Immediate Window), the full commands are `System.Web.HttpRuntime.AppDomainAppPath` and `System.Web.HttpRuntime.HttpContext.Server.MapPath("~")` – testpattern May 12 '17 at 11:41
  • 3
    @testpattern `HttpContext` is `System.Web.HttpContext.Current.Server.MapPath("~")`, not have `HttpRuntime` – chengzi Sep 06 '18 at 04:33
  • In a static method, use HostingEnvironment.MapPath("~") instead. – Rob Apr 04 '22 at 11:26
125

Use this code:

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

Detailed Reference:

Server.MapPath specifies the relative or virtual path to map to a physical directory.

  • Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
  • Server.MapPath("..") returns the parent directory
  • Server.MapPath("~") returns the physical path to the root of the application
  • Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

An example:

Let's say you pointed a web site application (http://www.example.com/) to

C:\Inetpub\wwwroot

and installed your shop application (sub web as virtual directory in IIS, marked as application) in

D:\WebApps\shop

For example, if you call Server.MapPath in following request:

http://www.example.com/shop/products/GetProduct.aspx?id=2342

then:

Server.MapPath(".") returns D:\WebApps\shop\products
Server.MapPath("..") returns D:\WebApps\shop
Server.MapPath("~") returns D:\WebApps\shop
Server.MapPath("/") returns C:\Inetpub\wwwroot
Server.MapPath("/shop") returns D:\WebApps\shop

If Path starts with either a forward (/) or backward slash (), the MapPath method returns a path as if Path were a full, virtual path.

If Path doesn't start with a slash, the MapPath method returns a path relative to the directory of the request being processed.

Note: in C#, @ is the verbatim literal string operator meaning that the string should be used "as is" and not be processed for escape sequences.

Footnotes

Server.MapPath(null) and Server.MapPath("") will produce this effect too.

CDspace
  • 2,639
  • 18
  • 30
  • 36
Harshal Doshi Jain
  • 2,567
  • 1
  • 20
  • 16
  • 26
    Perfect copy of https://stackoverflow.com/questions/275781/server-mappath-server-mappath-server-mappath-server-mappath#275791 – GGO Oct 09 '17 at 15:35
  • 4
    @GGO Good link! Following the links, I ended up doing `System.Web.Hosting.HostingEnvironment.MapPath("~")` instead, which works great, with no dependency on `System.Web.HttpContext.Current` – Max Barraclough Jun 15 '18 at 15:36
7

For dot net 6 I use:

AppContext.BaseDirectory

Cool thing about that is that it will be the same on asp.net and also on a console application.

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • string baseDirectory = AppContext.BaseDirectory; // it returns the bin path e.g. D:\WebMVCApp1\bin\Debug\netcoreapp3.1\ – Toan NC Mar 09 '22 at 07:54