Search code examples
c#visual-studio-2019folderbrowserdialogookii

Folder Dialog return to dialog if criteria not met


I'm using the Ookii Vista folder dialog for ease of use, and I have this code going on which is called from a button:

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

private string LoadDirectories()
{
    VistaFolderBrowserDialog fbd = new VistaFolderBrowserDialog();
    if (fbd.ShowDialog() == DialogResult.OK && fbd.SelectedPath.Contains("U000"))
    {
        return fbd.SelectedPath;
    }
    else
    {
        MessageBox.Show("Folder must be root assembly, which has U000 in name");
    }
    
    return string.Empty;
}

Currently, if the user does not pick a folder with U000 in the name, the dialog will close, but I want it to stay open until user either cancels or sets the correct folder path. I can't find this anywhere.


Solution

  • A simple realization of your needs (Add a while):

    Used nuget: Ookii.Dialogs.Wpf

    private string LoadDirectories()
    {
        VistaFolderBrowserDialog fbd = new VistaFolderBrowserDialog();
        while (true)
        {
            if (fbd.ShowDialog() == false)
            {
                break;
            }
            else if (fbd.SelectedPath.Contains("U000"))
            {
    
                return fbd.SelectedPath;
            }
            else
            {
                MessageBox.Show("Folder must be root assembly, which has U000 in name");
            }
        }
        return string.Empty;
    }
    

    output:

    enter image description here