Tony Cheetham
Tony Cheetham

Reputation: 967

C# WriteAllBytes ignores character encoding

I'm using the following code:

File.WriteAllBytes("c:\\test.xml", Encoding.UTF8.GetBytes("THIS IS A TEST"))

Which should in theory write a UTF8 file, but I just get an ANSI file. I also tried this just to be especially verbose;

File.WriteAllBytes("c:\\test.xml", ASCIIEncoding.Convert(ASCIIEncoding.ASCII, UTF8Encoding.UTF8, Encoding.UTF8.GetBytes("THIS IS A TEST")))

Still the same issue though.

I am testing the outputted files by loading in TextPad which reads the format correctly (I tested with a sample file as I know these things can be a bit weird sometimes)

Upvotes: 6

Views: 5270

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063944

WriteAllBytes isn't ignoring the encoding - rather: you already did the encoding, when you called GetBytes. The entire point of WriteAllBytes is that it writes bytes. Bytes don't have an encoding; rather: encoding is the process of converting from text (string here) to bytes (byte[] here).

UTF-8 is identical to ASCII for all ASCII characters - i.e. 0-127. All of "THIS IS A TEST" is pure ASCII, so the UTF-8 and ASCII for that are identical.

Upvotes: 9

Related Questions