我有一个正在尝试调试的 .NET 应用程序,我的应用程序的一部分从我的项目中加载了一个文件。该文件位于
C:\Users\USER_FOLDER\Documents\Visual Studio 2012\Projects\MY_PROJECT\_templates\myFile.html
在我的代码中,我指定了文件的相对路径并使用 DirectoryInfo 类获取文件的完整目录路径:
string myFile = (new DirectoryInfo("_templates/myFile.html")).FullName;
但是,这会返回以下路径(额外的\作为转义字符):
"C:\\Program Files\\IIS Express\\_templates\\myFile.html"
我原以为在 IIS Express 中调试时返回的路径会匹配我列出的第一个路径,而不是第三个。为什么是这样?我是否需要在我的项目中设置其他内容以使其正确导出路径?我假设如果我将代码部署到 IIS7 站点就不会发生这种情况,但我还没有达到该测试级别。
最佳答案
使用Server.MapPath :
Server.MapPath("~/_templates/myFile.html")
HttpServerUtility.MapPath("~/_templates/myFile.html")
关于c# - 使用 IIS Express 进行调试时获取文件的完整路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13295260/