45

I'm not able to get the current physical path within Application_Start using

HttpContext.Current.Request.PhysicalApplicationPath

because there is no Request object at that time.

How else can I get the physical path?

0xKayvan
  • 368
  • 1
  • 4
  • 18
Alex
  • 75,813
  • 86
  • 255
  • 348

10 Answers10

53
 protected void Application_Start(object sender, EventArgs e)
 {
     string path = Server.MapPath("/");
     //or 
     string path2 = Server.MapPath("~");
     //depends on your application needs

 }
rick schott
  • 21,012
  • 5
  • 52
  • 81
32

I created a website with ASP.Net WebForms where you can see the result of using all forms mentioned in previous responses from a site in Azure.

http://wfserverpaths.azurewebsites.net/

Summary:


Server.MapPath("/") => D:\home\site\wwwroot\

Server.MapPath("~") => D:\home\site\wwwroot\

HttpRuntime.AppDomainAppPath => D:\home\site\wwwroot\

HttpRuntime.AppDomainAppVirtualPath => /

AppDomain.CurrentDomain.BaseDirectory => D:\home\site\wwwroot\

HostingEnvironment.MapPath("/") => D:\home\site\wwwroot\

HostingEnvironment.MapPath("~") => D:\home\site\wwwroot\
batressc
  • 1,499
  • 1
  • 18
  • 28
  • 1
    How can I get: \\myserver\inetpub\wwwroot\myfolder instead of C:\inetpub\wwwroot\myfolder.... – Si8 Mar 30 '17 at 19:32
  • 1
    Select an option above and write this small code: `var pathComponents = AppDomain.CurrentDomain.BaseDirectory.Split(':'); pathComponents[0] = @"\\" + System.Environment.MachineName; var resultPath = string.Join("", pathComponents);` – batressc Jul 11 '17 at 16:20
23

You can also use

HttpRuntime.AppDomainAppVirtualPath
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
DotNetUser
  • 6,494
  • 1
  • 25
  • 27
  • 10
    For the physical path, you'd actually want `HttpRuntime.AppDomainAppPath` which "Gets the physical drive path of the application directory for the application hosted in the current application domain." `HttpRuntime.AppDomainAppVirtualPath` actually gets a virtual path such as "/MyApp". Either way, using HttpRuntime is the best way to get the application physical path, because it's available in every context, including static contexts even before an `HttpApplication` object is available. – Triynko Jul 31 '13 at 22:44
  • 1
    The other solutions posted here require an `HttpServerUtility` instance to be accessed through the `Server` property, which is available only within the context of an application event like "Application_Start" or within an active web request. – Triynko Jul 31 '13 at 22:47
  • `HttpRuntime.AppDomainAppPath` – Mehdi Khademloo Mar 27 '17 at 18:36
17

Use Server.MapPath("~")

   

Satpal
  • 132,252
  • 13
  • 159
  • 168
TJF
  • 2,248
  • 4
  • 28
  • 43
  • 4
    This works better than Server.MapPath("/"); because the path of the web application might not be the same as the root application one. – Nicolas De Irisarri Feb 13 '13 at 14:49
  • 9
    And `HttpRuntime.AppDomainAppPath` works better than any of these, because it works in any context including static contexts, whereas all other options require an `HttpServerUtility` instance to be accessed through the `Server` property, which is available only within the context of an application event like "Application_Start" or within an active web request. – Triynko Jul 31 '13 at 22:49
10

You can use this code:

AppDomain.CurrentDomain.BaseDirectory

Robert
  • 5,278
  • 43
  • 65
  • 115
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36
5

Best choice is using

AppDomain.CurrentDomain.BaseDirectory

because it's in the system namespace and there is no dependency to system.web

this way your code will be more portable

Manoochehr Dadashi
  • 715
  • 1
  • 10
  • 28
3

use below code

server.mappath() in asp.net

application.startuppath in c# windows application

Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195
maxy
  • 438
  • 3
  • 10
  • 20
3

There is also the static HostingEnvironment.MapPath

Alex
  • 721
  • 6
  • 11
2

System.AppDomain.CurrentDomain.BaseDirectory

This will give you the running directory of your application. This even works for web applications. Afterwards, you can reach your file.

royhowie
  • 11,075
  • 14
  • 50
  • 67
hakan
  • 3,284
  • 2
  • 19
  • 25
1

There's, however, slight difference among all these options which

I found out that

If you do

    string URL = Server.MapPath("~");

or

    string URL = Server.MapPath("/");

or

    string URL = HttpRuntime.AppDomainAppPath;

your URL will display resources in your link like this:

    "file:///d:/InetPUB/HOME/Index/bin/Resources/HandlerDoc.htm"

But if you want your URL to show only virtual path not the resources location, you should do

    string URL = HttpRuntime.AppDomainAppVirtualPath; 

then, your URL is displaying a virtual path to your resources as below

    "http://HOME/Index/bin/Resources/HandlerDoc.htm"        
Jenna Leaf
  • 2,255
  • 21
  • 29