钢铁战歌,qq个性情侣头像,亲亲我的混血王子
asp.net读取txt文件(记事本)内容:
using system; using system.collections; using system.configuration; using system.data; using system.web; using system.web.security; using system.web.ui; using system.web.ui.htmlcontrols; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.io; //获取txt文件流 namespace test { public partial class text : system.web.ui.page { protected void page_load(object sender, eventargs e) { response.write(getinteridlist("asp.txt")); } //读取txt文件的内容 public string getinteridlist(string strfile) { string strout; strout = ""; if (!file.exists(system.web.httpcontext.current.server.mappath(strfile))) { } else { streamreader sr = new streamreader(system.web.httpcontext.current.server.mappath(strfile), system.text.encoding.default); string input = sr.readtoend(); sr.close(); strout = input; } return strout; } } }
读取txt文件内容就是获取文件流,记得要引用using system.io;。
asp.net写入txt文件(记事本):
string txtpath = server.mappath("~\\public\\attinfo\\") + "test.txt"; streamwriter sw = new streamwriter(txtpath, false, system.text.encoding.default); sw.writeline("hello world"); sw.writeline(""); //输出空行 sw.writeline("asp.net网络编程 - 移动技术网!"); sw.close();
注意:如果写入记事本不需换行,可以使用 write,需要换行的,可以使用 writeline。
asp.net保存txt文件(记事本):
public void processrequest(httpcontext context) { context.response.clear(); context.response.buffer = true; //server.urlencode 防止保存的文件名乱码 context.response.addheader("content-disposition", "attachment;filename=" + context.server.urlencode("消费明细" + string.format("{0:yyyymmddhhmmss}", system.datetime.now) + ".txt")); context.response.contenttype = "text/plain"; string message = "hello world"; //如果导出的文件要换行,用environment.newline message += "hello world" + environment.newline; context.response.write(message); //停止页面的执行 context.response.end(); }
注意3点:
1.保存文件名乱码问题:用server.urlencode编码
2.txt文件中的换行问题:environment.newline
3.调用可以用js:window.location.href="download.ashx" 或window.open("download.ashx")
以上就是关于txt文件的相关操作,如果我的文章对你有帮助,就点个赞吧。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论