使用IIS Express进行调试时获取文件的完整路径

Ani*_*tha 7 .net c# iis directoryinfo iis-express

我有一个.NET应用程序,我正在尝试调试,我的部分应用程序从我的项目加载一个文件.此文件位于

C:\Users\USER_FOLDER\Documents\Visual Studio 2012\Projects\MY_PROJECT\_templates\myFile.html
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我指定了文件的相对路径,并使用DirectoryInfo类来获取文件的完整目录路径:

string myFile = (new DirectoryInfo("_templates/myFile.html")).FullName;
Run Code Online (Sandbox Code Playgroud)

但是,这将返回以下路径(额外的\作为转义字符):

"C:\\Program Files\\IIS Express\\_templates\\myFile.html"
Run Code Online (Sandbox Code Playgroud)

我期待IIS Express中的调试与我列出的第一条路径匹配时返回的路径,而不是第三条路径.为什么是这样?我需要在项目中设置其他东西以使其正确地获得路径吗?我假设如果我将代码部署到IIS7站点,这不会发生,但我还没有达到测试级别.

Kna*_*ģis 7

使用Server.MapPath:

Server.MapPath("~/_templates/myFile.html")
Run Code Online (Sandbox Code Playgroud)

HttpServerUtility.MapPath:

HttpServerUtility.MapPath("~/_templates/myFile.html")
Run Code Online (Sandbox Code Playgroud)

  • 不知道这是否是完全相同的情况,但是我有相同的对象引用问题。我使用HttpContext解决了它。如-HttpContext.Current.Server.MapPath(); (3认同)