简体   繁体   中英

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.

Use Server.MapPath :

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

or HttpServerUtility.MapPath :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM