选择文件夹对话框:WPF中似乎没有打开文件夹对话框,不过可以通过winform的方法打开,调研之前需要引用System.Windows.Forms;

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows.Forms; 7 8 namespace WordsSearch 9 { 10 //使用方法 11 //定义委托 12 //public delegate void OpenFolderDialogEventHandler(object sender, OpenFolderDialogEventArgs e); 13 ////用event 关键字声明事件对象 14 //public event OpenFolderDialogEventHandler OpenFolderDialogEvent; 15 ////事件触发方法 16 //private void OnOpenFileDialogEvent(object sender, System.EventArgs e) 17 //{ 18 // OpenFolderDialogEventArgs ea = new OpenFolderDialogEventArgs(this.textblock_filename); 19 // if (OpenFolderDialogEvent != null) 20 // OpenFolderDialogEvent(this, ea); 21 //} 22 23 /// <summary> 24 /// 选择文件夹事件 25 /// </summary> 26 public class OpenFolderDialogEventArgs : EventArgs 27 { 28 //文件夹路径 29 private string FolderPath; 30 31 /// <summary> 32 /// 事件处理过程 33 /// </summary> 34 /// <param name="tb">显示路径地址的文本框</param> 35 public OpenFolderDialogEventArgs(System.Windows.Controls.TextBox tb) 36 { 37 //new选择对话对象 38 FolderBrowserDialog m_Dialog = new FolderBrowserDialog(); 39 DialogResult result = m_Dialog.ShowDialog(); 40 if (result == System.Windows.Forms.DialogResult.Cancel) 41 { 42 return; 43 } 44 //取出当前选择路径 45 FolderPath = m_Dialog.SelectedPath.Trim(); 46 //将值赋予相应的文本框 47 tb.Text = FolderPath; 48 } 49 50 //事件属性 51 public string folderPath 52 { 53 get { return FolderPath; } 54 } 55 } 56 }
浙公网安备 33010602011771号