陈斌彬的技术博客

Stay foolish,stay hungry

OpenFileDialog 显示文件内容

创建 Windows 应用程序,添加textBox,Button

img

选择textBox的小坐标

img

选择 MultiLine

img

主要代码实现:

private void button1_Click(object sender, EventArgs e)
     {
         string fName;
         OpenFileDialog openFileDialog = new OpenFileDialog();//实例化
         openFileDialog.InitialDirectory = @"c:\";//打开的默认路径
         openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
         openFileDialog.RestoreDirectory = true;
         openFileDialog.FilterIndex = 1;
         if (openFileDialog.ShowDialog() == DialogResult.OK)
         {
             fName = openFileDialog.FileName;
             StreamReader sr = new StreamReader(@fName, System.Text.Encoding.GetEncoding("UTF-8"));
             string duqu;
             duqu = sr.ReadToEnd();
             this.textBox1.Text = duqu;
         }
     }
 }

启动调试

img

img