0

I have an ASP.NET MVC 4 applicaton.

Also I have a XML file located on the root of my application.

Whenever administrator changes this file, I want the applicaton know about it and read the new values.

...
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Changed += new FileSystemEventHandler(OnChanged);
...

private void OnChanged(object source, FileSystemEventArgs e) {
    var path = HttpContext.Current.Server.MapPath("~/"))
}

Every time HttpContext.Current seems to be null. How to get the root folder of my application, if this does not work?

Jaanus
  • 16,161
  • 49
  • 147
  • 202

3 Answers3

0

How to get the root folder of my application, if this does not work?

AppDomain.CurrentDomain.BaseDirectory
Joe
  • 122,218
  • 32
  • 205
  • 338
0

just what you need,

var domainPath = HttpRuntime.AppDomainAppPath;

https://stackoverflow.com/questions/6861368/httpcontext-current-server-mappath-object- reference-not-set-to-an-instance-of-an

Community
  • 1
  • 1
bashkan
  • 464
  • 1
  • 5
  • 14
0

And yet another alternative; HostingEnvironment.MapPath. What Server.MapPath does, without an active HttpContext.

See What is the difference between Server.MapPath and HostingEnvironment.MapPath?

in reality, the only difference is that you are allowed to pass null to Server.MapPath(), but not to HostingEnvironment.MapPath()

Community
  • 1
  • 1
sisve
  • 19,501
  • 3
  • 53
  • 95