1

I'm looking for a way to convert FQDN path: http://myserver.mydomain.com/mysite/myvirtualfolder/somefile.txt to physical path: c:\some\folder\somefile.txt.

My C# code is running on the same host where the IIS virtual Folder and IIS are located.

Is there any way to make such translation?

Pupsik
  • 741
  • 3
  • 8
  • 25
  • 2
    you should use MapPath: https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx – developer May 21 '18 at 15:51
  • If I understand the question correctly you won't be able to use `MapPath()` as that only works on the site which the code is running under. Check this comment... https://stackoverflow.com/a/11884444/1023618 – Reinstate Monica Cellio May 21 '18 at 16:07
  • @Archer Yes, you cannot specify a path outside of the Web application. That is mentioned clearly in the MSDN link from developer's comment above. But what makes you think it won't work for the OP? The file is clearly within the web application. – Racil Hilan May 21 '18 at 16:21
  • @RacilHilan I misunderstood the question, so it's not actually that clear. Either way, they got what they needed :) – Reinstate Monica Cellio May 21 '18 at 19:51

1 Answers1

1

You can use the MapPath method. If you use it in the ode-behind file for a web page:

string path = Server.MapPath("/mysite/myvirtualfolder/somefile.txt");

If you use it within a class that is not in the code-behind file:

string path = HttpContext.Current.Server.MapPath("/mysite/myvirtualfolder/somefile.txt");
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55