FileInfo.OpenText() Fails To Read Special Characters E.G. üò°
.net, fileinfo, opentext, utf-8, wpf
Solution
`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.
Problem
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