StreamWriter and UTF-8 Byte Order Marks

后端 未结 8 905
走了就别回头了
走了就别回头了 2020-11-27 19:02

I\'m having an issue with StreamWriter and Byte Order Marks. The documentation seems to state that the Encoding.UTF8 encoding has byte order marks enabled but when files are

8条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 19:50

    As someone pointed that out already, calling without the encoding argument does the trick. However, if you want to be explicit, try this:

    using (var sw = new StreamWriter(this.Stream, new UTF8Encoding(false)))
    

    The key is to construct a new UTF8Encoding(false), instead of using Encoding.UTF8Encoding. That's to control if BOM should be added or not.

    This is the same as calling StreamWriter without the encoding argument, internally it's just doing the same thing.

提交回复
热议问题