怎样打开一个记事本文件并显示在winform窗体的textbox空间里

如题所述

第1个回答  推荐于2016-08-30
1:记得引入命名空间 using System.IO;
2:textbox控件点击又上角的三角形选择multiiLine.即为多行显示。

OpenFileDialog op = new OpenFileDialog();
if(op.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
StreamReader read = new StreamReader(op.FileName, Encoding.Default);
textBox2.Text = read.ReadToEnd();
read.Close();
}本回答被提问者和网友采纳
第2个回答  2012-04-23
向窗体添加一个openfiledialog和一button
button双击事件中添加显示openfiledialog对话框代码,如this.openfiledialog1.ShowDialog()
读取文件string tempstr=File.ReadAllText(this.openfiledialog1.FileName)
或者string[] tempstr=File.ReadAllLine(this.openfiledialog1.FileName)
然后this.Text=tempstr
第3个回答  2012-04-23
这个要改环境变量,将打开文件设置为你自己的WINFORM程序,或者点右键 选择打开方式,选择到你自己的WINFORM程序
第4个回答  2012-04-23
FileStream ss = new FileStream(MyfileName, FileMode.Open);
StreamReader dd = new StreamReader(ss,Encoding.GetEncoding("gb2312"));
str1 = dd.ReadToEnd();
this.textBox1.Text = str1;

MyfileName 文件夹路径+文件名字.txt \

注意 textbox的属性 MulitiLine属性设置成 true
scrollBars 设置成Vertical
第5个回答  2012-04-23
FileStream fs1 = new FileStream(MyfileName, FileMode.Open);
StreamReader sr = new StreamReader(fs1,Encoding.GetEncoding("gb2312"));
str1 = sr.ReadToEnd();
this.textBox1.Text = str1;
sr.Close();
fs1.Close();

MyfileName 文件夹路径+文件名字.txt \

注意 textbox的属性 MulitiLine属性设置成 true
scrollBars 设置成Vertical
相似回答