加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 04:00:55 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试打开一个文件,其中包含我的C#正在运行的Web目录中的一些数据.基本上只是把它变成一个字符串.我试着做以下…… string email = File.ReadAllText("/orderforms/email_templates/file_to_include.txt"); 我不确定这是否是正确的方法,但似乎存在路径
我正在尝试打开一个文件,其中包含我的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"))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读