private void Form1_Load(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();            Type type = Type.GetType("System.Text.Encoding");
            PropertyInfo[] properties = type.GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (property.PropertyType == typeof(System.Text.Encoding))
                {
                    comboBox2.Items.Add(property.Name);
                }
            }
            comboBox2.Text = "UTF8";            //注册事件
            comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
        } private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            dialog.Title = "请选择文件名";
            dialog.CheckFileExists = true;
            dialog.Filter = ".txt|*.txt";
            dialog.FileOk += new CancelEventHandler(dialog_FileOk);
            dialog.ShowDialog();        }
 void dialog_FileOk(object sender, CancelEventArgs e)
        {
            richTextBox1.Text = "";
            OpenFileDialog dialog = sender as OpenFileDialog;
            if (dialog != null)
            {
                using (FileStream fs = File.OpenRead(dialog.FileName))
                {
                    using (StreamReader reader = new StreamReader(fs, Encoding.GetEncoding(comboBox2.Text)))
                    {
                        while (reader.Peek() != -1)
                        {
                            richTextBox1.AppendText(reader.ReadLine() + Environment.NewLine);
                        }
                    }
                }
            }
        }
想通过Encoding.GetEncoding(comboBox2.Text)来获取Encoding,但提示错误,ComboBox2下面的项为UTF8等等,但GetEncoding(string name)里面的参数只能接受utf-8这样的字符串。

解决方案 »

  1.   

    单击Button时弹出打开文件对话框,根据ComboBox中所选的编码方式,读取文件并显示在RichTextBox中
      

  2.   

    解决了。
        public partial class Form1 : Form
        {
            IList<EncodeObject> list = new List<EncodeObject>();        public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                comboBox1.Items.Clear();            Type type = Type.GetType("System.Text.Encoding");
                Assembly ass = type.Assembly;
                PropertyInfo[] properties = type.GetProperties();
                
                foreach (PropertyInfo property in properties)
                {
                    if (property.PropertyType == type)
                    {
                        MethodInfo method = property.GetGetMethod(true);
                        Encoding ec = (Encoding)method.Invoke(new object(), null);
                        list.Add(new EncodeObject(property.Name,ec));
                    }
                }
                comboBox1.DisplayMember = "Name";
                comboBox1.ValueMember = "Encoding";
                comboBox1.DataSource = list;
                
            }        private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.Multiselect = false;
                dialog.Filter = "*.txt|*.txt";
                dialog.FileOk += new CancelEventHandler(dialog_FileOk);
                dialog.ShowDialog();
            }        void dialog_FileOk(object sender, CancelEventArgs e)
            {
                OpenFileDialog dialog = sender as OpenFileDialog;
                if (dialog != null)
                {
                    richTextBox1.Text = "";                string FileName = dialog.FileName;
                    Encoding encoding = (Encoding)comboBox1.SelectedValue;                using (FileStream fs = File.OpenRead(FileName))
                    {
                        using (StreamReader reader = new StreamReader(fs,encoding))
                        {
                            while (reader.Peek() != -1)
                            {
                                this.richTextBox1.AppendText(reader.ReadLine() + Environment.NewLine);
                            }
                        }
                    }            }
            }        
        }    public class EncodeObject
        {
            private string _name;
            private Encoding _ec;        public string Name
            {
                get { return _name; }
                set { _name = value; }
            }        public Encoding Encoding
            {
                get { return _ec; }
                set { _ec = value; }
            }        public EncodeObject(string name, Encoding ec)
            {
                this._name = name;
                this._ec = ec;
            }
        }