如何通过代码更改文本文件的编码?我正在使用此代码实际创建文件本身,但是如何更改编码(更改为不带BOM的UTF-8)
string path = @"E:\Test\Example.txt";
if (!File.Exists(path))
{
File.Create(path);
}
您可以执行类似的操作,但是我不确定这是否真的有意义,因为您的文件中似乎没有内容...
如果您有内容,请替换string.Empty
为内容
File.WriteAllText(path, string.Empty, Encoding.GetEncoding(<someEncodingCode>));
编辑 :
File.WriteAllText(path, string.Empty, new UTF8Encoding(false));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句