简体   繁体   English

如何检查 FolderBrowserDialog 的内容是否为空?

[英]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.我在 WPF 项目中使用FolderBrowserDialog并且它工作正常,我想检查所选文件夹的内容selectedPath是否为空或 null 和现有文件的扩展名。

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);
}
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.");

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM