FileInfo.OpenText()无法读取特殊字符EG UO° [英] FileInfo.OpenText() Fails To Read Special Characters E.G. üò°
问题描述
有中显示的字符在文本框和TextBlock的一些文本文件。
Have some text files that display many characters as � in TextBox and TextBlock.
我怎样才能正确地在.NET WPF读取并显示这些文件?
How can I properly read and display these files in .NET WPF?
文件读取其中Fi是一个FileInfo。
File read where fi is a FileInfo.
fileText = fi.OpenText().ReadToEnd();
在WPF我得到的字符üO°和其它特殊字符。
In WPF I get the � character for ü ò ° and other special characters.
尝试过多种字体。
文化是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公司()似乎在阅读完所有的特殊字符为字节253
OpenText() appears to be reading all the special characters as byte 253
我觉得我学到的是文本不是UTF8 EN codeD。 UTF8采用128-255控制。 OpenText的()用于UTF8编码。 WikiUFT8
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
推荐答案
fi.OpenText
打开的StreamReader
与UTF8编码。如果你需要不同的编码,用这个来代替:
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();
当然,你实际上并不需要的的FileInfo
目标可言,因为只有路径正在由上面的调用。
Of course, you don't actually need the FileInfo
object at all, as only the path is being used by the above call.
这篇关于FileInfo.OpenText()无法读取特殊字符EG UO°的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!