You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ookii Vista文件夹对话框时不满足路径条件如何保持弹窗开启

解决方案

通过VistaFolderBrowserDialog自带的FileOk事件可直接实现路径校验拦截,无需关闭对话框后重复弹出,用户体验最优:

using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Ookii.Dialogs;

private string LoadDirectories()
{
    VistaFolderBrowserDialog fbd = new VistaFolderBrowserDialog();
    // 绑定确认前的校验事件
    fbd.FileOk += (s, e) =>
    {
        if (!fbd.SelectedPath.Contains("U000"))
        {
            // 取消关闭操作,对话框保持开启
            e.Cancel = true;
            MessageBox.Show("Folder must be root assembly, which has U000 in name");
        }
    };

    if (fbd.ShowDialog() == DialogResult.OK)
    {
        return fbd.SelectedPath;
    }
    return string.Empty;
}

实现原理

FileOk事件触发于用户点击对话框「确定」按钮后、对话框正式关闭前,将事件参数的Cancel属性设置为true即可阻止对话框关闭,用户可直接在当前打开的对话框中重新选择路径。

备选兼容方案(循环弹出)

如果受限于Ookii包版本无法使用FileOk事件,可采用循环逻辑重复弹出对话框直到符合要求或用户主动取消:

private string LoadDirectories()
{
    while (true)
    {
        VistaFolderBrowserDialog fbd = new VistaFolderBrowserDialog();
        var result = fbd.ShowDialog();
        // 用户主动取消直接返回空
        if (result != DialogResult.OK)
        {
            return string.Empty;
        }
        // 路径符合要求直接返回
        if (fbd.SelectedPath.Contains("U000"))
        {
            return fbd.SelectedPath;
        }
        // 不符合要求提示后重新弹出对话框
        MessageBox.Show("Folder must be root assembly, which has U000 in name");
    }
}

内容的提问来源于stack exchange,提问作者Jaymar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 15:36:04