Search code examples
c#wpffolderbrowserdialog

How can I check the content of the FolderBrowserDialog if is it empty or not?


I'm using FolderBrowserDialog in a WPF project and it works fine, I would like to check the content of the folder selected selectedPath if is it empty or null and the extension of the existed files.

How can I do that?

try
{
    using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
    {
        System.Windows.Forms.DialogResult result = dialog.ShowDialog();
        FileText.Text = dialog.SelectedPath;
    }
}
catch (Exception exp)
{
    Console.WriteLine("Error : " + exp);
}

Solution

  • using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
    {
        System.Windows.Forms.DialogResult result = dialog.ShowDialog();
        if (result == System.Windows.Forms.DialogResult.OK || result == System.Windows.Forms.DialogResult.Yes)
        {
            FileText.Text = dialog.SelectedPath;
            var directory = new System.IO.DirectoryInfo(dialog.SelectedPath);
    
            var files = directory.GetFiles(); // Array with information about files.
    
            if (files.Length == 0)
                Debug.WriteLine("Empty Folder.");
            else
            {
                var filesTxt = files.Where(f => f.Extension == ".txt").ToArray(); // Array with information about TXT files.
                if (filesTxt.Length == 0)
                    Debug.WriteLine("There is no TXT files in the folder.");
    
            }
        }
    }