创建 Windows 应用程序,添加textBox,Button
选择textBox的小坐标
选择 MultiLine
主要代码实现:
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;
}
}
}
启动调试