// 读文件 public TextWriter w; // 写文件 public TextReader r; // 读文本文件 private void button1_Click(object sender, System.EventArgs e) { if(openFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = openFileDialog1.FileName; r = new StreamReader(openFileDialog1.FileName, System.Text.Encoding.Default, false, 512);//用当前默认的编码方式读取,否则将会出现乱码; richTextBox1.Text = r.ReadToEnd(); r.Close(); } } // 写文本文件 private void button2_Click(object sender, System.EventArgs e) { if(saveFileDialog1.ShowDialog() == DialogResult.OK) { textBox1.Text = saveFileDialog1.FileName; w = new StreamWriter(saveFileDialog1.FileName, false,System.Text.Encoding.Default , 512); w.Write(richTextBox1.Text); w.Flush(); w.Close(); } }