读取TXT文件内容的方法

时间:2021-05-25

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

namespaceaspnetcs
{
///<summary>
///WebForm1的摘要说明。
///</summary>
publicclassWebForm1:System.Web.UI.Page
{
privatevoidPage_Load(objectsender,System.EventArgse)
{

if(!File.Exists(MapPath("weather.txt")))
{
Console.WriteLine("{0}doesnotexist.",MapPath("weather.txt"));
return;
}
StreamReadersr=newStreamReader(MapPath("weather.txt"),System.Text.Encoding.Default);
Stringinput=sr.ReadToEnd();
sr.Close();

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

#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

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

}
#endregion

protectedvoidButton1_Click(objectsender,System.EventArgse)
{
Session["name"]=TextBox1.Text;
Response.Redirect("WebForm2.aspx");
}
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章