FileInfo.OpenText() Fails To Read Special Characters E.G. üò°

9.9k views Asked by At

Have some text files that display many characters as � in TextBox and TextBlock.

How can I properly read and display these files in .NET WPF?

File read where fi is a FileInfo.

fileText = fi.OpenText().ReadToEnd();

In WPF I get the � character for ü ò ° and other special characters.

Have tried multiple fonts.

Culture is en-EN.

If I read the file as a Stream I get the special characters

System.IO.Stream fsIn = fi.OpenRead();
if (fsIn.Length == 0) return;
int curInt = -1;
StringBuilder sb = new StringBuilder();
while ((curInt = fsIn.ReadByte()) >= 0)
{
    sb.Append((char)curInt);
} 

OpenText() appears to be reading all the special characters as byte 253

What I think I have learned is the text is not UTF8 encoded. UTF8 uses 128-255 for control. OpenText() is used for UTF8 encoding. WikiUFT8

2

There are 2 answers

0
Simon MᶜKenzie On BEST ANSWER

fi.OpenText opens a StreamReader with UTF8 encoding. If you need different encoding, use this instead:

using (var reader = new StreamReader(fi.FullName, Encoding.Unicode))
    fileText = reader.ReadToEnd();

Of course, you don't actually need the FileInfo object at all, as only the path is being used by the above call.

4
500 - Internal Server Error On

You've essentially answered your own question. You text file is not in Unicode format - it's probably ANSI in some specific code page. To read such files you can use a StreamReader with an encoding passed in. There are some samples here.