ASP.NET C#从web路径中的文件中获取所有文本



我正试图打开一个文件,该文件包含我的C#正在运行的web目录中的一些数据。基本上只是把它变成一根绳子。我试着做了以下。。。

string email = File.ReadAllText("/orderforms/email_templates/file_to_include.txt");

我不确定这是否是正确的方法,但似乎存在路径问题,整个路径将根据它运行的web服务器而改变。

这是目录设置。。。

/Classes/Page.ascx.cs  (the page that tries to read the text from the
file)
/orderforms/<one of multiple pages execute the above class here or in a sub directory
/orderforms/email_templates/file_to_include.txt
/orderforms/email_templates/file_to_include2.txt

我应该使用什么路径和函数将文件的所有内容读取为字符串?

感谢

试试这个:

string email = File.ReadAllText(Server.MapPath("~/orderforms/email_templates/file_to_include.txt"))

您需要使用Server.MapPath:

http://msdn.microsoft.com/en-us/library/ie/ms524632(v=vs.90).aspx

最新更新