实例:
winform中选择一个文件并且能够读取到txt文件中的具体内容,主要熟悉winform的文件选择的相关操作。
namespace flieselect { public partial class FormMain : Form { public FormMain() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //打开文件对话框 OpenFileDialog of = new OpenFileDialog(); //选择框标题 of.Title = "请选择文件"; //设置可多选 of.Multiselect = true; //设置对话框起始目录 of.InitialDirectory = @"D:\"; //文件类型筛选字符串 of.Filter = "所有文件|*.*|文本文件|*.txt|视频文件|*.wav"; //展示对话框 if (of.ShowDialog() ==DialogResult.OK ) { string path = of.FileName; this.textBox2.Text = of.FileName; FileStream fs = new FileStream(path, FileMode.Open); StreamReader sr = new StreamReader(fs); this.textBox1.Text = sr.ReadToEnd(); sr.Close(); fs.Close(); } } }