讀寫文本文件之StreamReader和StreamWriter


private string _filePath = @"1.txt";

//查詢文件是否存在,如果不存在,則創建

if (!File.Exists(_filePath))

{
using (File.Create(_filePath))
{

}
}

using (StreamReader sr = new StreamReader(_filePath))
{
//string str = sr.ReadLine();//讀一行
string str = sr.ReadToEnd();//讀到底
if (!string.IsNullOrWhiteSpace(str))
{
txtRtsp.Text = str;
}
}

//保存地址:
using (StreamWriter sw = new StreamWriter(_filePath)) //將覆蓋原有內容
//using (StreamWriter sw = new StreamWriter(_filePath,true))//在原有內容上追加 
{
sw.Write(rtsp);
}

//循環讀多行

String line;
                           while ((line = sr.ReadLine()) != null) 
                {
                    this.ListBox1.Items.Add("line ");   //增加讀出的內容到listbox            
                    i++;
                }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM