7

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

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

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

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

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

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

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

4

1 回答 1

7

使用Server.MapPath

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

HttpServerUtility.MapPath

HttpServerUtility.MapPath("~/_templates/myFile.html")
于 2012-11-08T18:23:56.520 回答