2

I want to save a file when my application ends.

For that, I need to get the root folder of the application.

The only way I know to do that is using Server.MapPath("~"), but this does not work, even through HttpContext.Current.Server because there is not current context, as far as I understand.

Is there a smart & simple way to get that folder path? Will I have permission problems, trying to create a file in the root folder?

Any alternative ideas how to accomplish this?

Pankaj
  • 9,749
  • 32
  • 139
  • 283
nir.arazi
  • 63
  • 10

2 Answers2

7

You should use HttpRuntime.AppDomainAppPath, which will always work.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1
  1. Request.MapPath("")
  2. Request.PhysicalApplicationPath
  3. HttpRuntime.AppDomainAppPath

Edit

It should be HttpRuntime.AppDomainAppPath only as the other two option will be null in Application_End handler.

Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • It is there. I am using VS 2010. Web Application. In Global.asax.cs file under `Application_End` handler, you can type the above mentioned code...Right ? – Pankaj Mar 12 '12 at 18:18
  • Yes, but it will be `null`. This is why he's asking the question in the first place. – SLaks Mar 12 '12 at 18:20
  • +1. Thanks. You are right. I actually could not figure out how to fire the Application_End event initially. – Pankaj Mar 12 '12 at 18:28