Silverlight读取文件加载到内存(StreamReader中文乱码问题)

使用System.IO.Stream与System.IO.StreamReader可以像普通C#程序一样读取,
但是中文无法正常显示,目前的办法是把文件编码保存成utf-8

大气象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Text;

namespace HCLoad
{
    
public partial class UC_ReadFile : UserControl
    {
        
public UC_ReadFile()
        {
            InitializeComponent();
        }
        
//Silverlight读取文件加载到内存(StreamReader中文乱码问题)
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog opfdlg 
= new OpenFileDialog();
            opfdlg.Multiselect 
= false;
            
/************************************************
             * 不管打开什么格式的文件都需要与StreamReader编码对应。
             * 即如果使用System.Text.Encoding.UTF8,就需要把文件保存成utf-8格式。
             ***********************************************
*/
            opfdlg.Filter 
= "TXT Files (*.txt)|*.txt";
            
//opfdlg.Filter = "XML Files (*.xml)|*.xml";
            
//opfdlg.Filter = "HCLX Files (*.hclx)|*.hclx";
            bool bResult = (bool)opfdlg.ShowDialog();

            System.IO.FileInfo info 
= opfdlg.File;
            System.IO.Stream fs 
= info.OpenRead();

            
/************************************************
             * Silverlight不支持gb2312,据说可以通过转化为字节码的方式。
             * 
http://blogs.msdn.com/feiyu/archive/2009/12/08/support-gb2312-in-silverlight-net-framework.aspx
             * BinaryReader reader = new BinaryReader(dataStream);
             * byte[] byteArray = reader.ReadBytes(.....
             * 我们拿到了没有编码的字节,但是我们知道其中的内容是用GB2312编码的。
             * 下一步用查表的方式把字节转化成Unicode;
             * 最后在用Encoding.Unicode.GetChars()得到Unicode的字节和字符串。 
             ***********************************************
*/
            System.IO.StreamReader sr 
= new System.IO.StreamReader(fs, System.Text.Encoding.UTF8);
            
//System.IO.StreamReader sr = new System.IO.StreamReader(fs, System.Text.Encoding.Unicode);

            MessageBox.Show(sr.ReadToEnd());
            sr.Close();
            fs.Close();
            
/**************************************************************************
             * 非Silverlight环境下,可以使用普通的编码方式。
              StreamReader reader = new StreamReader("test.txt", System.Text.Encoding.GetEncoding("gb2312"));
              //或用这个编码格式
              //StreamReader reader = new StreamReader("test.txt",System.Text.Encoding.Default);               
              //读取流
              this.textBox1.Text = reader.ReadToEnd();
              //关闭流
              reader.Close();

              //再或者 直接用这个方式
              //richTextBox1.LoadFile("D:\\5.txt", RichTextBoxStreamType.RichText);
             ************************************************************************
*/
        }
    }
}

 

 

posted @ 2010-07-02 15:39  大气象  阅读(3514)  评论(7编辑  收藏  举报
http://www.tianqiweiqi.com