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; }