当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 读取TXT文件内容的方法

ASP.NET
如何在ASP.NET中使用SmtpMail发送邮件
在VB.NET中利用Split和Replace函数计算字数
Attribute应用:简化ANF自定义控件初始化过程
ASP.NET 2.0移动开发入门之使用样式
ASP.NET 2.0中使用OWC生成图表
ASP.NET 2.0中控件的简单异步回调
一个无法捕获ADO.NET Dataset的内存错误
深入解读ADO.NET2.0的十大最新特性
.Net平台下的分布式缓存设计
ASP.NET全局异常处理浅析
ASP.NET 2.0中文验证码的实现
浅析.NET平台编程语言的未来走向
.net 框架程序设计收藏
使用ASP.NET MVC Futures 中的异步Action
详解.NET中的XmlReader与XmlWriter
关于.NET中的Server push技术
asp.net页面执行机制
对比JSP和ASP.NET的存储过程
.NET 4.0不会包含System.Shell.CommandLine
ASP.NET十个有效性能优化的方法

ASP.NET 中的 读取TXT文件内容的方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 51 ::
收藏到网摘: n/a

复制代码 代码如下:
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");
}
}
}