C#操作文本文件是如何实现的呢?让我们开始讲述吧:
- using System.IO;
- //声明控件
- protected System.Web.UI.HtmlControls.HtmlTextArea txtValue;
C#操作文本文件之读取文本文件:
- //主程序
- 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();
C#操作文本文件之写入文本文件:
- //主程序
- 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();
C#操作文本文件的基本实现内容就向你介绍到这里,希望对你了解和学习C#操作文本文件有所帮助。
【编辑推荐】