读取TXT文件内容的方法

所属分类: 网络编程 / ASP.NET 阅读数: 467
收藏 0 赞 0 分享

复制代码 代码如下:
using System; 
using System.Collections; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Web; 
using System.Web.SessionState; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.HtmlControls; 
using System.IO;\\这是必须的 

namespace aspnetcs 

 /// <summary> 
 /// WebForm1 的摘要说明。 
 /// </summary> 
 public class WebForm1 : System.Web.UI.Page 
 { 
   private void Page_Load(object sender, System.EventArgs e) 
  { 

   if (!File.Exists(MapPath("weather.txt")))  
   { 
    Console.WriteLine("{0} does not exist.", MapPath("weather.txt")); 
    return; 
   } 
   StreamReader sr = new StreamReader(MapPath("weather.txt"), System.Text.Encoding.Default); 
   String input = sr.ReadToEnd(); 
   sr.Close(); 

   input = input.Replace("\r\n", "<br>").Replace("\n", "<br>"); 
   Response.Write(input); 
   Response.End();//就是这上面的代码,如果用Response.WriteFile方法,虽然能读出txt文件内容,却不能显示分段格式 
   // 在此处放置用户代码以初始化页面 
  } 

  #region Web 窗体设计器生成的代码 
  override protected void OnInit(EventArgs e) 
  { 
   // 
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 
   // 
   InitializeComponent(); 
   base.OnInit(e); 
  } 

  /// <summary> 
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改 
  /// 此方法的内容。 
  /// </summary> 
  private void InitializeComponent() 
  {     
   this.Load += new System.EventHandler(this.Page_Load); 

  } 
  #endregion 

  protected void Button1_Click(object sender, System.EventArgs e) 
  { 
    Session["name"]=TextBox1.Text; 
    Response.Redirect("WebForm2.aspx"); 
  } 
 } 

更多精彩内容其他人还在看

Asp.net中的mail的发送

Asp.net中的mail的发送
收藏 0 赞 0 分享

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享
查看更多