using System.IO;
private void button1_Click(object sender, EventArgs e) { string strFilePath = ""; OpenFileDialog fd = new OpenFileDialog(); fd.Filter = "文本文件(*.txt)|*.txt|All files (*.*)|*.*"; //过滤文件类型 //fd.InitialDirectory = Application.StartupPath + "\\Temp\\";//设定初始目录 //fd.ShowReadOnly = true; //设定文件是否只读 if (fd.ShowDialog() != DialogResult.OK) { return; } strFilePath = fd.FileName; if (!File.Exists(strFilePath)) { MessageBox.Show("抱歉!文件不存在,请重新输入文件名!"); return; } StreamReader strReader = new StreamReader(new FileStream(strFilePath, FileMode.Open, FileAccess.ReadWrite), Encoding.Default); //string strTemp = strReader.ReadLine(); string strTemp = strReader.ReadToEnd(); strReader.Close(); textBox1.Text = strTemp; }