创建 Windows 应用程序,添加textBox,Button
![img](http://ww3.sinaimg.cn/mw690/78f9859egw1eusqoiuj4mj20b409fwew.jpg)
选择textBox的小坐标
![img](http://ww4.sinaimg.cn/mw690/78f9859egw1eusqoj5wh2j208903fwej.jpg)
选择 MultiLine
![img](http://ww4.sinaimg.cn/mw690/78f9859egw1eusqojis0oj209508u0t3.jpg)
主要代码实现:
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](http://ww1.sinaimg.cn/mw690/78f9859egw1eusqsb0tjkj20gn0c9div.jpg)
![img](http://ww3.sinaimg.cn/mw690/78f9859egw1eusqsdn0ybj2082082aa8.jpg)