使用 System.IO.File 類寫入檔案

你可以使用 System.IO.File.WriteAllText 函式將字串寫入檔案。

string text = "String that will be stored in the file";
System.IO.File.WriteAllText(@"C:\MyFolder\OutputFile.txt", text);

你還可以使用 System.IO.File.WriteAllLines 函式接收 IEnumerable<String> 作為第二個引數(與前一個示例中的單個字串相對)。這使你可以從一系列行中寫入內容。

string[] lines = { "My first string", "My second string", "and even a third string" };
System.IO.File.WriteAllLines(@"C:\MyFolder\OutputFile.txt", lines);