textBox 控件 ,设置文本框可输入多行 ,Multiline 属性 设为 true
Anchor 属性 (锚) ,Top , Left ,Bottom, Right ,
Dock 属性 (停靠), Fill ,Botton ,Top 等
menuStrip 主菜单 (&N) ShortcutKeys (Ctrl +N) 与菜单项关联的快捷键
OpenFileDialog 控件
Title 属性 标题
Filter 属性 (筛选器) ”文本文件|*.txt“
ShowDialog() 使用 DialogResult 接收
读取一个文本文件步骤(5步)
第一步 申明一个文件流 FileStream
new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
文件名 ,对文件的操作 , 第三个配对使用 ,别人对此文件的访问
第二步 创建读取器 StreamReader
EndOfStream 属性 ,是否读到流的末尾
第三步 读操作 ReadTOEnd()
第四步 关闭读取器 Close()
第五步 关闭文件流 Close()
Encoding 编码取值
代码如下
//新建
private void tsmnsNewFile_Click(object sender, EventArgs e)
{
txtText.Text = "";
}
//打开
private void tsmnsOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开"; //标题
ofd.Filter = "文本文件|*.txt|所有文件|*.*"; //过滤器
if (ofd.ShowDialog() == DialogResult.OK) //点击打开
{
//读一个文本文件步骤 (5步)
//(1.申明一个文件流)
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
//2.创建读取器
StreamReader sr = new StreamReader(fs,Encoding.Default);
//sr.ReadLine();//读取一行
//while(!sr.EndOfStream)
//{
// string line = sr.ReadLine(); //读取一行
// txtText.Text = txtText.Text + line + "\r\n"; // \t 表示回车符
//}
//3. 读操作
txtText.Text=sr.ReadToEnd(); //读取到最后
//4. 关闭读取器
sr.Close();
//5. 关闭文件流
fs.Close();
}
ofd.ShowDialog();
}
Anchor 属性 (锚) ,Top , Left ,Bottom, Right ,
Dock 属性 (停靠), Fill ,Botton ,Top 等
menuStrip 主菜单 (&N) ShortcutKeys (Ctrl +N) 与菜单项关联的快捷键
OpenFileDialog 控件
Title 属性 标题
Filter 属性 (筛选器) ”文本文件|*.txt“
ShowDialog() 使用 DialogResult 接收
读取一个文本文件步骤(5步)
第一步 申明一个文件流 FileStream
new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
文件名 ,对文件的操作 , 第三个配对使用 ,别人对此文件的访问
第二步 创建读取器 StreamReader
EndOfStream 属性 ,是否读到流的末尾
第三步 读操作 ReadTOEnd()
第四步 关闭读取器 Close()
第五步 关闭文件流 Close()
Encoding 编码取值
代码如下
//新建
private void tsmnsNewFile_Click(object sender, EventArgs e)
{
txtText.Text = "";
}
//打开
private void tsmnsOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "打开"; //标题
ofd.Filter = "文本文件|*.txt|所有文件|*.*"; //过滤器
if (ofd.ShowDialog() == DialogResult.OK) //点击打开
{
//读一个文本文件步骤 (5步)
//(1.申明一个文件流)
FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
//2.创建读取器
StreamReader sr = new StreamReader(fs,Encoding.Default);
//sr.ReadLine();//读取一行
//while(!sr.EndOfStream)
//{
// string line = sr.ReadLine(); //读取一行
// txtText.Text = txtText.Text + line + "\r\n"; // \t 表示回车符
//}
//3. 读操作
txtText.Text=sr.ReadToEnd(); //读取到最后
//4. 关闭读取器
sr.Close();
//5. 关闭文件流
fs.Close();
}
ofd.ShowDialog();
}