0

We are using the File.CreateText method which according to MSDN:

Creates or opens a file for writing UTF-8 encoded text. If the file already exists, its contents are overwritten.

(https://learn.microsoft.com/en-us/dotnet/api/system.io.file.createtext?view=netframework-4.5.2)

When we promote this code into other environments the output file is correctly encoded as UTF-8

But for one environment, the same method is producing a file encoded as UTF-8 ByteOrderMark and causing mayhem.

I understand the most direct fix should be to use StreamWriter and input UTF8Encoding(False) into the constructor

But I don't understand why using File.CreateText only causes an issue/creates UTF-8 with BOM in one specific environment and not the others

Can someone please help explain this to me?

user2727893
  • 97
  • 1
  • 9
  • 1
    I don't see how that is possible. `CreateText` creates a `StreamWriter` with with the following `this(path, append, UTF8NoBOM, DefaultBufferSize)` source here https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/mscorlib/system/io/streamwriter.cs#L124 its the same for framework – TheGeneral Mar 08 '21 at 06:48
  • 1
    if this were the case it would represent a bug, However, the source code is pretty clear cut. I would put together a minimal example, and test, and prove it categorically. From there we could test it on a vm ect, or you could go to github and create a bug post. Though, my gut feeling is something isn't what you think it is, and it is unlikely to be a bug (given the source) – TheGeneral Mar 08 '21 at 06:52
  • 1
    Unless... you are using an older framework, and a bug has been fixed merged and released (for which I cant find). – TheGeneral Mar 08 '21 at 06:57
  • 1
    Maybe BOM in your input? https://stackoverflow.com/questions/25752230/encode-text-in-c-sharp-utf-8-without-bom – Genusatplay Mar 08 '21 at 07:04

0 Answers0