using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;//流的命名空间
namespace 对话框
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// openFileDialog1.Filter = "文本文件|(*.txt)|所以文件|*.*|视频文件|*.avi";//每两个是一组,筛选文件后缀名
openFileDialog1.Filter = "视频文件|*.avi;*.mp4;*.mp3;*.txt;*.jpg";//多个需要用";"隔开
//openFileDialog1.ShowDialog();//显示对话框
DialogResult isok = openFileDialog1.ShowDialog();//枚举返回鼠标点击的选项值
if (isok==DialogResult.OK)//判断是否点的确定按钮
{
string s = openFileDialog1.FileName;//获取选中的文件的文件路径
//MessageBox.Show(s);
//使用流进行文件读取
StreamReader sr = new StreamReader(s,Encoding.Default);//读取文件路径的文件,设置编码方式
textBox1.Text = sr.ReadToEnd();
sr.Close();//用完流必须关掉,不然其他地方没法写流,会一直被占用
}
}
private void button2_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "文本文件|*.txt";
DialogResult isok = saveFileDialog1.ShowDialog();
if (isok==DialogResult.OK)
{
string s = saveFileDialog1.FileName;
//使用流保存 //流是程序和硬盘之间的通道,路径就是硬盘上的地址,打通一个通道,按这个地址
//去读取内容或者存储内容
StreamWriter sw = new StreamWriter(s);//创建一个缓存区,在这个路径下存储文本内容
sw.Write(textBox1.Text);//把文本内容写入这个路径存储区
sw.Close();//关闭流
}
}
}
}