Get current application physical path within Application_Start

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

 }

Method 2

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:homesitewwwroot

Server.MapPath("~") => D:homesitewwwroot

HttpRuntime.AppDomainAppPath => D:homesitewwwroot

HttpRuntime.AppDomainAppVirtualPath => /

AppDomain.CurrentDomain.BaseDirectory => D:homesitewwwroot

HostingEnvironment.MapPath("/") => D:homesitewwwroot

HostingEnvironment.MapPath("~") => D:homesitewwwroot

Method 3

You can also use

HttpRuntime.AppDomainAppVirtualPath

Method 4

Use Server.MapPath("~")

   

Method 5

You can use this code:

AppDomain.CurrentDomain.BaseDirectory

Method 6

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

Method 7

There is also the static
HostingEnvironment.MapPath

Method 8

use below code

server.mappath() in asp.net

application.startuppath in c# windows application

Method 9

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.

Method 10

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"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x