• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    ASP.NET对txt文件相关操作(读取、写入、保存)
    POST TIME:2021-10-18 02:26

    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动态读取Excel文件最简方法
    • 如何在ASP.NET Core类库项目中读取配置文件详解
    • asp.net读取模版并写入文本文件
    • ASP.NET中上传并读取Excel文件数据示例
    • asp.net读取excel文件的三种方法示例
    • asp.net读取磁盘文件、删除实例代码
    • ASP.NET(C#)读取Excel的文件内容
    • asp.net 读取配置文件方法
    • C#/.NET读取或修改文件的创建时间及修改时间详解
    上一篇:DataGridView展开与收缩功能实现
    下一篇:ASP.NET搭配Ajax实现搜索提示功能
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信