读取TXT文件内容的方法

2016-02-19 11:06 88 1 收藏

今天天气好晴朗处处好风光,好天气好开始,图老师又来和大家分享啦。下面给大家推荐读取TXT文件内容的方法,希望大家看完后也有个好心情,快快行动吧!

【 tulaoshi.com - Web开发 】

代码如下:
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("rn", "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"); 
  } 
 } 

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1596534.html

延伸阅读
标签: 软件教程 excel
说起Excel,那是大名鼎鼎、如雷贯耳啊!Excel已成为日常办公必备的软件之一,主要用来处理与表格相关的问题。在使用过程中难免会碰到一些棘手的问题,比如:Excel打不开,提示为不可读取内容,当遇到这些问题我们改怎么处理呢?经百度一下发现主要原因是因为引用了外部文档数据、指向了外部文档中的宏等等。不过我么可以通过修复临时解决此问题...
标签: ASP
  Many times we might need some part of code which will access all sub-folders of the server and also all      files within the sub-folder.      The following line of asp code will map to a specified folder and searches all the sub-folders    &nbs...
虽然进入win95之後,一般读写ini文件被读写Registry所取代,但我们还是可以透过 win31的传统方式读写ini文件,以存程式目前的相关设定,而於下一次程式执行时再读回来。目前建议使用GetSetting SaveSetting的方式存於Registry中,不用目前的方式。 储存程式的设定 '请於form中放3个TextBox,一个CommandBoxPrivate Declare Funct...
本例子为读取配置文件,一般用作读取参数 /*  * Created on 2004-9-21  *  * Java读取配置文件例子 */ package net.jspcn.test; import java.util.*; import java.io.*; /**  * @author whilvydy  * ...
今天和夜月兄讨论了一下在windows nt/2000/xp下如何读取bios信息,现在把结果向大家汇报一下。 大家都知道,windows接管了对物理内存的直接存取,而bios信息存在物理内存的f000:0000处,关键就是如何读取物理内存。 查阅了msdn的文章后,发现以下有几个函数和物理内存访问有关: NTSTATUS ZwOpenSection(OUT PHANDLE Sec...

经验教程

339

收藏

81
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部