Published on

C# StreamWriter write file line by line

因為要寫一個小工具需要逐行寫入檔案,突然想到之前有寫過兩篇文章 C# - Simple StreamWriter and StreamReaderC# - Simple FileStream Write and Read,結果好像沒有逐行寫檔的程式碼,簡單的記錄一下使用 StreamWriter 來逐行寫檔

同步

var list = new List<string>();
using var sw = new StreamWriter(path, true);

foreach (var line in list)
{
    sw.WriteLine(line);
}

sw.Flush();
sw.Close();

非同步

var list = new List<string>();
await using var sw = new StreamWriter(path, true);

foreach (var line in list)
{
	await sw.WriteLineAsync(line);
}

await sw.FlushAsync();
sw.Close();

圖片來源 Unsplash