我正在尝试读取包含一些丹麦字符的文本文件。我已经找到了几种使用不同类型编码的方法,但我看到的例子只是读取一个文件。这就是我到目前为止所拥有的。
//search directory for all .txt files
foreach (string files in Directory.GetFiles(@"C:ftpinbox", "*.txt"))
{
//The 'using' command close connection when it is done
using (var reader = new StreamReader(File.OpenRead(files)))
{
// Handle contents
}
}
我的问题是所有的字符都被读取为�.我需要把它们照原样读进去。
我通过更改来解决问题
using (var reader = new StreamReader(File.OpenRead(files)))
进入
using (var reader = new StreamReader(files, System.Text.Encoding.GetEncoding("iso-8859-1")))
现在,丹麦语字符被按原样读入。