无为清净楼资源网 Design By www.qnjia.com
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文件的相关操作,如果我的文章对你有帮助,就点个赞吧。
标签:
ASP.NET,txt文件
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 金佩姗.1997-喜新恋旧【现代派】【WAV+CUE】
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 陈晓东.1997-在乎你感受(EP)【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《云水禅心 - 佛曲古筝礼赞1-2》1CD[MP3][190MB]
- 纯音入心系列纯音乐《精心传神-古筝》1CD[MP3][363.1MB]
- 纯音入心系列纯音乐《琴瑟古风之古筝精选曲》1CD[MP3][354.1MB]
- 陈晓东.1997-在乎你感受(EP)【宝丽金】【WAV+CUE】
- 黄凯芹.1990-给你留念(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《古筝佛曲》1CD[MP3][411.1MB]
- 纯音入心系列纯音乐《古筝世界名曲》1CD[MP3][38.9MB]
- 纯音入心系列纯音乐《古筝新奏-菩提妙音》1CD[MP3][112.3MB]
- 陈思安.1993-怀念福建金曲5CD【歌丽美】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】