从StreamReader读取文件时C#编码问题(问号) [英] c# encoding problems (question marks) while reading file from StreamReader

查看:75
本文介绍了从StreamReader读取文件时C#编码问题(问号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Windows Phone应用读取.txt文件时遇到问题.

I've a problem while reading a .txt file from my Windows Phone app.

我已经制作了一个简单的应用程序,可以从.txt文件中读取流并进行打印.

I've made a simple app, that reads a stream from a .txt file and prints it.

很不幸,我来自意大利,我们有很多带重音的字母.这就是问题所在,实际上所有带重音的字母都被打印为问号.

Unfortunately I'm from Italy and we've many letters with accents. And here's the problem, in fact all accented letters are printed as a question mark.

这是示例代码:

var resourceStream = Application.GetResourceStream(new Uri("frasi.txt",UriKind.RelativeOrAbsolute));
            if (resourceStream != null)
            {
                {
                    //System.Text.Encoding.Default, true
                    using (var reader = new StreamReader(resourceStream.Stream, System.Text.Encoding.UTF8))
                    {
                        string line;
                        line = reader.ReadLine();

                        while (line != null)
                        {
                            frasi.Add(line);
                            line = reader.ReadLine();       
                        } 
                    }
                }

所以,我问你如何避免这种情况.

So, I'm asking you how to avoid this matter.

祝一切顺利.

解决方案:我不确定文件是否以UTF-8编码-我用正确的编码保存了文件,但它的工作原理像一个魅力.谢谢奥斯卡

Solution: I didn't make sure the file was encoded in UTF-8- I saved it with the correct encoding and it worked like a charm. thank you Oscar

推荐答案

您需要使用Encoding.Default.更改:

You need to use Encoding.Default. Change:

using (var reader = new StreamReader(resourceStream.Stream, System.Text.Encoding.UTF8))

using (var reader = new StreamReader(resourceStream.Stream, System.Text.Encoding.Default))

这篇关于从StreamReader读取文件时C#编码问题(问号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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