C# 文本文件 ANSI編碼格式 轉 UTF8
如果用 Encoding.Convert()把ANSI格式的字節數組轉成 UTF8格式的字節數組,然后用FileStream 去wirte字節數組,其結果為丟BOM,即保存為 UTF8 without BOM。
解決方法,用 StreamWriter,直接wirte string 即可。
using (StreamWriter fs = new StreamWriter(tmpPath + "info.txt", false, Encoding.UTF8)) { fs.Write(string.Format("{0}\r\n{1}", txt, string.Join("\r\n", info))); fs.Flush(); fs.Close(); }