Get full path to file while debugging using IIS Express

Anil Natha picture Anil Natha · Nov 8, 2012 · Viewed 14.2k times · Source

I have a .NET application that I am trying to debug and part of my application loads a file from my project. This file is located at

C:\Users\USER_FOLDER\Documents\Visual Studio 2012\Projects\MY_PROJECT\_templates\myFile.html

In my code, I specify a relative path to the file and use the DirectoryInfo class to get the full directory path to my file:

string myFile = (new DirectoryInfo("_templates/myFile.html")).FullName;

However, this returns the following path (extra \'s as escape characters):

"C:\\Program Files\\IIS Express\\_templates\\myFile.html"

I was expecting the path that is returned when debugging in IIS Express would match the first path I listed, not the third. Why is this? Is there something else that I need to set up in my project to have it derive the paths properly? I'm assuming that this would not happen if I deployed my code to a IIS7 site, but I haven't gotten to that testing level yet.

Answer

Knaģis picture Knaģis · Nov 8, 2012

Use Server.MapPath:

Server.MapPath("~/_templates/myFile.html")

or HttpServerUtility.MapPath:

HttpServerUtility.MapPath("~/_templates/myFile.html")