使用Web应用程序中的相对路径读取文件的内容 [英] Read contents of a file using a relative path in a Web Application
本文介绍了使用Web应用程序中的相对路径读取文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用相对路径/ URL读取Web应用程序中文本文件的内容?
How can I read the contents of a text file in my Web Application, using a relative path/URL?
文件位于根目录中我的应用程序。
The files are located in a directory at the root of my application.
我不想指定我想要访问的文件的完整路径,因为我的测试环境不是我生产环境的精确镜像。
I don't want to specify the full path to the file I want to access, since my test environment is not an exact mirror of my production environment.
推荐答案
使用 Server.MapPath(/ path / to / file)
并将结果传递给 File.ReadAllText()
:
String template = File.ReadAllText(Server.MapPath("~/Templates/") + filename);
这篇关于使用Web应用程序中的相对路径读取文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文