Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get full path to file while debugging using IIS Express

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.

like image 704
Anil Natha Avatar asked Nov 08 '12 18:11

Anil Natha


1 Answers

Use Server.MapPath:

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

or HttpServerUtility.MapPath:

HttpServerUtility.MapPath("~/_templates/myFile.html")
like image 185
Knaģis Avatar answered Oct 17 '22 12:10

Knaģis