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这样的字符串。
{
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这样的字符串。
解决方案 »
- C# 给文件和文件夹写保护
- csv文件查询
- 如何取listView第二列的值
- [菜鸟问题]WinForm里,如何在DataGrid中把某个字段0和1的值显示为“是”和“否”
- 画面中的表格生成excel文件后,画面中按钮不可用?
- datagrid按分类排序的问题(这几天做足球网,会问到很多问题,都是100分,大家支持啊)
- 我在VS2010中把光标放在函数名上 会出现关于函数的提示 在后面有个(+1重载)是什么意思?
- 基类对象赋值给继承类对象的问题
- 执行程序后,输入数据n,在panel上建立n个TextBox(),在TextBox输入数据,但无法取出
- winform 视频控件 保存时卡死
- TreeView 的问题~~ 大家帮忙那个看看啊~
- 关于hashtable序列化的问题
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;
}
}