C#操作文本文件實例淺析
作者:佚名
C#操作文本文件實例向你講述了C#操作文本文件的具體操作的過程,希望對你了解C#操作文本文件有所幫助。
C#操作文本文件是如何實現(xiàn)的呢?讓我們開始講述吧:
- using System.IO;
- //聲明控件
- protected System.Web.UI.HtmlControls.HtmlTextArea txtValue;
C#操作文本文件之讀取文本文件:
- //主程序
- FileStream fsInfo = new FileStream(
- "文件路徑(在項目內(nèi)的)", 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(
- 文件路徑(在項目內(nèi)的)", 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#操作文本文件的基本實現(xiàn)內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#操作文本文件有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來源:
bianceng.cn