您的位置:首页 > 编程语言 > C#

c#操作文本文件

2006-03-13 14:21 351 查看
using System.IO;

//声明控件
protected System.Web.UI.HtmlControls.HtmlTextArea txtValue;

读取文本文件:


//主程序
FileStream fsInfo = new FileStream( "文件路径(在项目内的)", FileMode.Open, FileAccess.Read );
StreamReader srInfo = new StreamReader( fsInfo, System.Text. Encoding.GetEncoding( "GB2312" ) );
srInfo.BaseStream.Seek( 0, SeekOrigin.Begin );
txtValue.Value = " ";
string strLine = srInfo.ReadInfo();
while( strLine != null )
{
txtValue.Value += strLine + "/n";
strLine = srInfo.ReadLine();
}
srInfo.Close();

写入文本文件:

//主程序
FileStream fsInfo = new FileStream( 文件路径(在项目内的)", FileMode.OpenOrCreate, FileAccess.Write );
StreamWriter swInfo = new StreamWriter( fsInfo );
swInfo.Flush();
swInfo.BaseStream.Seek( 0, SeekOrigin.Begin );
swInfo.Write( txtValue.Value );

swInfo.Flush();
swInfo.Close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: