OpenFileDialog:可以打开指定后缀名的文件,既能单个打开文件也能批量打开文件
/// <summary>/// 批量打开文档/// 引用:System.Window.Fomrs.OpenFileDialog/// </summary>public void OpenFile(){OpenFileDialog dialog = new OpenFileDialog(){FileName = "Select a text file",//是否允许选择多个文件 默认falseMultiselect = false,//对话框标题Title = "添加图片",//指定文件过滤器,如果不设置Filter则表示所有文件都能打开。Filter = "*.jpg,*.jpeg,*.png,*.bmp|*.jpg;*.jpeg;*.png;*.bmp;"//可以同时显示*.jpg,*.jpeg,*.png,*.bmp 图片文档//Filter = "文本文件|*.txt|Word文档|*.doc|所有文件|*.*"//选择只显示.txt或.doc文档 或 显示全部文档};//获取对话框返回结果if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK){label1.Content = dialog.FileName;}}/// <summary>/// 批量打开文档/// 引用:System.Window.Fomrs.OpenFileDialog/// </summary>public void OpenFiles(){OpenFileDialog dialog = new OpenFileDialog(){FileName = "Select a text file",Multiselect = true,Title = "添加图片",Filter = "*.jpg,*.jpeg,*.png,*.bmp|*.png;*.jpg;*.jpeg;*.bmp;"//Filter = "文本文件|*.txt|Word文档|*.doc|所有文件|*.*"};//获取对话框返回结果if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK){foreach (string fileName in dialog.FileNames){Console.WriteLine("已选择文件:" + fileName);}}}