如何读取、修改文本文件
2006-12-07 14:41
411 查看
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.Text;
using System.IO;
namespace CommonFunction
{
/// <summary>
/// operTxt 的摘要说明。
/// </summary>
public class operTxt : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtContent;
protected System.Web.UI.WebControls.Button btnRead;
protected System.Web.UI.HtmlControls.HtmlInputFile filePath;
protected string txtFilePath,fileExtName;
protected System.Web.UI.WebControls.Button btnRepaire;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
this.btnRepaire.Click += new System.EventHandler(this.btnRepaire_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnRead_Click(object sender, System.EventArgs e)
{
if(filePath.PostedFile.FileName != "")
{
txtFilePath =filePath.PostedFile.FileName;
fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".")+1,3);
if(fileExtName !="txt" && fileExtName != "TXT")
{
Response.Write("请选择文本文件");
}
else
{
StreamReader fileStream = new StreamReader(txtFilePath,Encoding.Default);
txtContent.Text = fileStream.ReadToEnd();
fileStream.Close();
}
}
}
private void btnRepaire_Click(object sender, System.EventArgs e)
{
if(filePath.PostedFile.FileName != "")
{
txtFilePath =filePath.PostedFile.FileName;
fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".")+1,3);
if(fileExtName !="txt" && fileExtName != "TXT")
{
Response.Write("请选择文本文件");
}
else
{
StreamWriter fileWrite = new StreamWriter(txtFilePath,false,Encoding.Default);
fileWrite.Write(txtContent.Text);
fileWrite.Close();
}
}
}
}
}
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.Text;
using System.IO;
namespace CommonFunction
{
/// <summary>
/// operTxt 的摘要说明。
/// </summary>
public class operTxt : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtContent;
protected System.Web.UI.WebControls.Button btnRead;
protected System.Web.UI.HtmlControls.HtmlInputFile filePath;
protected string txtFilePath,fileExtName;
protected System.Web.UI.WebControls.Button btnRepaire;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
this.btnRepaire.Click += new System.EventHandler(this.btnRepaire_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnRead_Click(object sender, System.EventArgs e)
{
if(filePath.PostedFile.FileName != "")
{
txtFilePath =filePath.PostedFile.FileName;
fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".")+1,3);
if(fileExtName !="txt" && fileExtName != "TXT")
{
Response.Write("请选择文本文件");
}
else
{
StreamReader fileStream = new StreamReader(txtFilePath,Encoding.Default);
txtContent.Text = fileStream.ReadToEnd();
fileStream.Close();
}
}
}
private void btnRepaire_Click(object sender, System.EventArgs e)
{
if(filePath.PostedFile.FileName != "")
{
txtFilePath =filePath.PostedFile.FileName;
fileExtName = txtFilePath.Substring(txtFilePath.LastIndexOf(".")+1,3);
if(fileExtName !="txt" && fileExtName != "TXT")
{
Response.Write("请选择文本文件");
}
else
{
StreamWriter fileWrite = new StreamWriter(txtFilePath,false,Encoding.Default);
fileWrite.Write(txtContent.Text);
fileWrite.Close();
}
}
}
}
}
相关文章推荐
- 如何读取、修改文本文件
- Fiddler:如何读取本地文件修改Request参数
- 如何自定义修改kettlle默认读取kettle.properties文件路径
- 使用PYTHON实现如何修改文本文件中的内容
- 读取扫描版的Pdf文件 如何修改扫描版的pdf文件
- 如何使用python修改文本文件
- js读取修改创建txt文本类型文件(.ini)
- 如何使用10M的内存读入100M的文件,并且保证在读取过程中可进行删除、修改和增加内容
- 请教Ado.Net按文本读取CSV/Txt文件时,如何禁止将内容转换成数字
- Linux如何修改环境变量PATH,以及系统登录时读取文件的顺序
- 使用System.IO来读取以及修改文本文件
- 如何让Hadoop读取以gz结尾的文本格式的文件
- 如何读取配置文件并连接后台数据库,且对数据库进行操作实例(查询或修改等)
- 如何让Hadoop读取以gz结尾的文本格式的文件
- C++文件读写操作(三)如何统计文本的行数及如何读取文件某一行内容
- C#如何从文件读取文本
- 如何读取超大容量文本txt文件
- 如何从文本(.txt)里面读取配置文件
- 在解决乱码问题是,如何修改文本文件的编码类型。
- 使用C++文件流如何实现 修改文本文件中的数据,再存回文件中的原有位置。。。