1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 //添加命名空间 8 using System.IO; 9 10 namespace 读取文本文件 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 //读取 17 using (StreamReader sr = new StreamReader(@"C:UsersAdministratorDesktop电影.txt", Encoding.Default)) 18 { 19 //Console.WriteLine(sr.ReadLine()); 20 while (!sr.EndOfStream) 21 { 22 Console.WriteLine(sr.ReadLine()); 23 } 24 } 25 26 27 //写入 28 //true 代表是追加文本还是覆盖文本 29 using (StreamWriter sw = new StreamWriter(@"C:UsersAdministratorDesktop ewnwe.txt", true)) 30 { 31 sw.Write("测试文本文段!"); 32 } 33 34 Console.ReadKey(); 35 } 36 } 37 }