使用ookii-dialogs-wpf在WPF选择文件夹时能输入路径

在进行WPF开发时,System.Windows.Forms.FolderBrowserDialog的选择文件夹功能不支持输入路径:

希望能够获得下图所示的选择文件夹功能:

于是,通过NuGet中安装Ookii.Dialogs.Wpf包,并创建一个简单的工具类:

https://github.com/ookii-dialogs/ookii-dialogs-wpf/tree/master/sample/Ookii.Dialogs.Wpf.Sample

csharp 复制代码
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;
        }
    }
}

调用该工具类:

csharp 复制代码
if (DialogUtil.GetDir("选择文件夹", out string dir))
{
	//to do
}
相关推荐
somethingGoWay14 小时前
wpf .netcore 导出docx文件
wpf·.netcore
somethingGoWay16 小时前
wpf .netcore 导出pdf文件
pdf·wpf·.netcore
xiaowu0801 天前
策略模式-不同的鸭子的案例
开发语言·c#·策略模式
VisionPowerful1 天前
九.弗洛伊德(Floyd)算法
算法·c#
ArabySide1 天前
【C#】 资源共享和实例管理:静态类,Lazy<T>单例模式,IOC容器Singleton我们该如何选
单例模式·c#·.net core
gc_22991 天前
C#测试调用OpenXml操作word文档的基本用法
c#·word·openxml
almighty271 天前
C#海康车牌识别实战指南带源码
c#·海康车牌识别·c#实现车牌识别·车牌识别源码·c#车牌识别
self_myth2 天前
[特殊字符] 深入理解操作系统核心特性:从并发到分布式,从单核到多核的全面解析
windows·macos·wpf·harmonyos
c#上位机2 天前
wpf之TextBlock
c#·wpf
almighty272 天前
C# WinForm分页控件实现与使用详解
c#·winform·分页控件·c#分页·winform分页