做一个记事本程序时用filestream读取汉字时出现乱码,而读取字符时不出错,代码如下,求高手帮忙。
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
byte[] b = new byte[500];
OpenFileDialog fi = new OpenFileDialog();
fi.InitialDirectory = @”d:\”;
fi.Filter = “txt files (*.txt)|*.txt|All files (*.*)|*.*”;
fi.FilterIndex = 1;
fi.RestoreDirectory = true;
if (fi.ShowDialog() == DialogResult.OK)
{
FileStream fs = new FileStream(fi.FileName, FileMode.Open);
fs.Seek(0, SeekOrigin.Begin);
fs.Read(b, 0, 500);
fs.Close();
string str = Encoding.UTF8.GetString(b);
textBox1.Text = str;
}
else
{
textBox1.Text = “”;
}
}