FileInfo.OpenText() Fails To Read Special Characte

2019-07-04 15:46发布

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条回答
该账号已被封号
2楼-- · 2019-07-04 16:29

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.

查看更多
迷人小祖宗
3楼-- · 2019-07-04 16:30

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.

查看更多
登录 后发表回答