C#创建UTF8无BOM文本文件


In order to omit the byte order mark (BOM), your stream must use a custom instance of UTF8Encoding instead of the default System.Text.Encoding.UTF8:

1.Call the UTF8Encoding constructor with False for the encoderShouldEmitUTF8Identifier parameter.

2.Pass the UTF8Encoding instance to the stream constructor.

实例:

复制代码
var utf8WithoutBom = new System.Text.UTF8Encoding(false);
                                                //^^^^^
using (var sink = new StreamWriter("Foobar.txt", false, utf8WithoutBom))
{
    sink.WriteLine("...");
}
复制代码

 

Note that omitting the BOM is only permissible for UTF-8, not for UTF-16.


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM