问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/10903120/fileinfo-opentext-fails-to-read-special-characters-e-g-%c3%bc%c3%b2