C# 生成文本文件 UTF8编码格式 不要带 BOM ,不知为什么,直接用Encoding.UTF8生成文件可能会带BOM,导致上传到服务器时有点问题,文件开始多出一个字符点,要强制生成不还BOM的UTF-8编码格式才行。
Encoding utf8WithoutBom = new UTF8Encoding(false);
string fn = (string)dr["FullName"];
string text = File.ReadAllText(fn, Encoding.UTF8);
text = text.Replace(keywords, replace);
//File.WriteAllText(fn, text.Trim(), Encoding.UTF8);
// 使用默认的UTF8编码(不带BOM)
Encoding utf8WithoutBom = new UTF8Encoding(false);
// 使用StreamWriter写入文件,并指定编码
using (StreamWriter writer = new StreamWriter(fn, false, utf8WithoutBom))
{
writer.Write(text);
}