讀取TXT文件內容的方法

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");
  }
 }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章