C# txt文件的读取与写入

时间:2020-12-11 13:21:47
C#创建记事本
方法一:
//创建对象
FileStream stream = new FileStream(@"d:\aa.txt",FileMode.Create);//fileMode指定是读取还是写入
StreamWriter writer = new StreamWriter(stream);
writer.WriteLine("123456");//写入一行,写完后会自动换行
writer.Write("abc");//写完后不会换行
writer.WriteLine("ABC");
writer.Close();//释放内存
stream.Close();//释放内存 方法二:
StreamWriter writer = new StreamWriter("d:\\aa.txt");
writer.WriteLine("123456");
writer.Write("abc");
writer.WriteLine("ABC");
writer.Close(); C#读取记事本文件
方法一:
FileStream stream = new FileStream(@"d:\aa.txt", FileMode.Open);
StreamReader reader = new StreamReader(stream);
//this.textbox1.Text = reader.ReadLine(); //一次性读取一行
this.textbox1.Text = reader.ReadToEnd(); //一次性读取全部数据
reader.Close();
stream.Close(); 方法二:
StreamReader reader = new StreamReader(@"d:\\aa.txt");
this.textbox1.Text = reader.ReadToEnd();
reader.Close();