C#读写txt文件操作方法大全

啥也不说了,直接上代码。

 

//按行读取txt文件

string[] contents = File.ReadAllLines("E:\\345.txt");

foreach (string i in contents)
{
textBox2.Text = textBox2.Text + i + "\r\n";
listBox1.Items.Add(i);

}


//按行把字符串写入到txt文件,并追加到文件尾
using (StreamWriter sw = File.AppendText("E:\\345.txt"))
{
sw.WriteLine("This");
sw.WriteLine("is Extra");
sw.WriteLine("Text");
}

posted @ 2021-03-04 10:57  杨咩咩plus  阅读(650)  评论(0)    收藏  举报