WindowsForm 流
流
包括输入流代码 输出流代码
流:
输入流:
string filename = openFileDialog1.FileName;
//通过读入流进行文件读取
StreamReader sr = new StreamReader(filename);
textBox1.Text = sr.ReadToEnd();
sr.Close();
输出流:
string filename = saveFileDialog1.FileName;
//写入流,可以在硬盘上创建文件,并为文件写入信息
StreamWriter sw = new StreamWriter(filename);
sw.Write(this.textBox1.Text);
sw.Close();
this:代表的它所在的那个类当前对象
首先亮相 在MSDN里找到
输入流:
记事本打开处用到
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) { DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { string filename = openFileDialog1.FileName; //通过读入流进行文件读取 StreamReader sr = new StreamReader(filename); textBox1.Text = sr.ReadToEnd(); sr.Close(); //写完要关闭 不然再用到就会出现问题 } }
输出流:
记事本 保存处用到
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) { if (FileName == null) { DialogResult dr = saveFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { string filename = saveFileDialog1.FileName; //写入流,可以在硬盘上创建文件,并为文件写入信息 StreamWriter sw = new StreamWriter(filename); sw.Write(this.textBox1.Text); sw.Close(); } } else { //写入流,可以在硬盘上创建文件,并为文件写入信息 StreamWriter sw = new StreamWriter(FileName); sw.Write(this.textBox1.Text); sw.Close(); //用完后关闭 } }