ASP.NET对txt文件相关操作(读
发布时间:2020-11-18 10:52:31 所属栏目:asp.Net 来源:互联网
导读:ASP.NET读取txt文件(记事本)内容:usingSystem;usingSystem.Collections;usingSystem.Configuration;
|
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("~PublicAttInfo") + "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 – 如何添加.aspx页面到现有的MVC 4项目?
- ASP.NET MVC3中的HTML反而不是JSON的IIS响应
- asp.net-mvc-4 – AngularJs,DropZone.Js,MVC4 – 拖放,预览
- ASP.NET中上传并读取Excel文件数据示例
- asp.net-mvc-4 – 全局过滤器MVC 4的执行顺序是什么
- asp.net-mvc-3 – “区域”文件夹中的样式,脚本和图像
- asp.net-mvc – 从MVC版本1迁移后,ASP.NET MVC 2 actionlin
- ASP.NET AJAX与ASP.NET MVC中的jQuery
- asp.net-mvc-4 – 如何在Kendo UI Grid中扩展页面加载时的所
- asp.net – 应用程序池在iisreset之后无法启动
推荐文章
站长推荐
- asp.net-mvc-4 – 最小和最大字符串长度的单独错
- asp.net-mvc – 使用Asp.net MVC 4中的OutputCac
- ASP.NET Core中实现用户登录验证的最低配置示例代
- asp.net – 我如何使用AJAX来确定用户的会话是否
- asp.net-mvc-4 – .net 4.5 ASP.Net web API JSO
- asp.net-mvc – 使用MVC3剃刀的ASP.Net图表控件
- 如何在asp.net中更改javascript警告框的标题?
- asp.net – 共享.NET应用程序池
- ASP.NET MVC AJAX文档在哪里?
- 如何在ASP.NET Web应用程序中打开一个SectionGro
热点阅读
