在进行WPF开发时,System.Windows.Forms.FolderBrowserDialog
的选择文件夹功能不支持输入路径:
希望能够获得下图所示的选择文件夹功能:
于是,通过NuGet中安装Ookii.Dialogs.Wpf
包,并创建一个简单的工具类:
https://github.com/ookii-dialogs/ookii-dialogs-wpf/tree/master/sample/Ookii.Dialogs.Wpf.Sample
using Ookii.Dialogs.Wpf;
using System.Windows.Forms;namespace Utils
{public class DialogUtil{public static bool GetDir(string description, out string dir, string rootDir = null){dir = null;if (VistaFolderBrowserDialog.IsVistaFolderDialogSupported){VistaFolderBrowserDialog val = new VistaFolderBrowserDialog();val.Description = description;val.SelectedPath = rootDir;val.Multiselect = (false);if (val.ShowDialog() == true){dir = val.SelectedPath;return true;}}else{FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();folderBrowserDialog.Description = description;if (folderBrowserDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)return false;dir = folderBrowserDialog.SelectedPath;return true;}return false;}}
}
调用该工具类:
if (DialogUtil.GetDir("选择文件夹", out string dir))
{//to do
}