Invalid characters in File.ReadAllText

c#, character-encoding, special-characters, symbols, text

Solution

This is likely due to a mismatch in the `Encoding`. Use the ReadAllText overload which allows you to specify the proper `Encoding` to use when reading the file.

The default overload will assume UTF-8 unless it can detect UTF-32. Any other encoding will come through incorrectly.

Problem

I'm calling `File.ReadAllText()` in a program designed to format some files that I have. Some of these files contain the `®` (174) symbol. However, when the text is being read, the returned string contains `�` (65533) symbols where the `®` (174) should be. What would cause this and how can I fix it?

Original source

Related problems