1 //浏览文件路径 2 3 private void button_Click(object sender, EventArgs e) 4 { 5 OpenFileDialog fileDialog = new OpenFileDialog(); 6 fileDialog.Multiselect = true; 7 fileDialog.Title = "请选择文件"; 8 fileDialog.Filter = "所有文件(*.txt*)|*.txt*"; 9 if(fileDialog.ShowDialog() == DialogResult.OK) 10 { 11 string file = fileDialog.FileName; 12 this.textBox1.Text = file; 13 } 14 this.fileName = this.textBox.Text; //这条语句中的fileName是我设置的全局变量,用来存文件路径的 15 } 16 17 18 19 //读取文件".txt" 20 21 this.richTextBox1.Text = File.ReadAllText(this.fileName,Encoding.Default); 22 23 24 25 //保存文件 26 27 File.AppendAllText(this.fileName, this.richTextBox1.Text);