C#帮助阅读使用StreamReader的外国字符 [英] C# Help reading foreign characters using StreamReader

查看:109
本文介绍了C#帮助阅读使用StreamReader的外国字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code读取包含外文字符的文本文件,该文件是EN codeD ANSI并在记事本看起来不错。在code以下无法正常工作,当读取和字符显示为正方形DataGrid中显示的文件中的值,莫不是另一个问题在其他地方?

I'm using the code below to read a text file that contains foreign characters, the file is encoded ANSI and looks fine in notepad. The code below doesn't work, when the file values are read and shown in the datagrid the characters appear as squares, could there be another problem elsewhere?

StreamReader reader = new StreamReader(inputFilePath, System.Text.Encoding.ANSI);
using (reader = File.OpenText(inputFilePath))

感谢

更新:我已经尝试了下,发现System.Text.Encoding所有编码。和所有无法正确显示文件。

Update: I have tried all encodings found under System.Text.Encoding. and all fail to show the file correctly.

更新2:我已经修改了文件编码(重新保存该文件)到UNI code和使用System.Text.Encoding.Uni code和它的工作就好了。那么,为什么记事本正确地读呢?而为什么没有System.Text.Encoding.Uni code读取ANSI文件格式?

Update 2: I've changed the file encoding (resaved the file) to unicode and used System.Text.Encoding.Unicode and it worked just fine. So why did notepad read it correctly? And why didn't System.Text.Encoding.Unicode read the ANSI file?

推荐答案

是的,这可能是与文件的实际编码,大概UNI code。尝试UTF-8作为其是单向code编码的最常见形式。否则,如果该文件ASCII当时的标准ASCII编码应该工作。

Yes, it could be with the actual encoding of the file, probably unicode. Try UTF-8 as that is the most common form of unicode encoding. Otherwise if the file ASCII then standard ASCII encoding should work.

这篇关于C#帮助阅读使用StreamReader的外国字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆