1、主要是使用 File 类来进行操作,因此必须要在C#程序中引用 using System.IO;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace AllConsole01 { class Program { static void Main(string[] args) { string path01 = @"D:\Temp\UnityTxtWriteLoad\ZheTest01.txt"; string[] theText1 = { "Hello", "This", "just", "test" }; // theText1的内容只是被添加到该文本一次,后续再用该语句来添加时会不断的覆盖之前的内容。 File.WriteAllLines(path01, theText1, Encoding.UTF8); string appendText1 = "This is extra text" + Environment.NewLine; // 随着时间的推移继续向文本中加入新的内容,只要不删除,文本内容就会越来越长 File.AppendAllText(path01, appendText1, Encoding.UTF8); // 读取文本中的内容 // 读取文本的所有行,并将每行保存成字符串数组的一个元素 string[] textRead = File.ReadAllLines(path01); foreach(string str in textRead) { Console.WriteLine(str); } Console.ReadKey(); } } }结果如下图:
参考资料:
[1] File Class