【问题标题】:FolderBrowserDialog nested foldersFolderBrowserDialog 嵌套文件夹
【发布时间】:2014-01-09 13:24:54
【问题描述】:

我有一个文件夹。该文件夹中有多个文件夹,每个文件夹中都有一个图像。像这样:

Main Folder>Subfolder 1>Picture 1

Main Folder>Subfolder 2>Picture 2

等等

我希望使用FolderBrowserDialog 选择主文件夹,并以某种方式显示子文件夹中的所有图片(图片 1、图片 2 等)FolderBrowserDialog?去做这个?谢谢

【问题讨论】:

    标签: c# folderbrowserdialog


    【解决方案1】:

    是的,这是可能的,但FolderBrowserDialog 只是解决方案的一部分。它可能看起来像这样:

    using (var fbd = new FolderBrowserDialog())
    {
        if (fbd.ShowDialog() == DialogResult.OK)
        {
            foreach (var file in Directory.GetFiles(fbd.SelectedPath,
                "*.png", SearchOption.AllDirectories)
            {
                // this catches things like *.png1 or *.pngp
                // not that they'd ever exist; but they may
                if (Path.GetExtension(file).Length > 4) { continue; }
    
                var pictureBox = new PictureBox();
                pictureBox.Load(file);
    
                // set its location here
    
                this.Controls.Add(pictureBox);
            }
        }
    }
    

    此代码仅搜索 png 文件,值得注意的是,我检查扩展名的原因是因为对 3 字符扩展名搜索鲜为人知的警告:

    文件扩展名正好为三个字符的 searchPattern 返回具有三个或更多字符扩展名的文件,其中前三个字符与 searchPattern 中指定的文件扩展名匹配。

    【讨论】:

      【解决方案2】:
      Directory.GetFiles("path", "*.jpeg", SearchOption.AllDirectories);
      

      【讨论】:

      【解决方案3】:

      使用文件夹浏览器对话框用户只能选择文件夹而不是文件。因此,您可以拥有自己的控件来显示所选文件夹中存在的图像列表。

              FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
              DialogResult dlgResult = folderBrowserDlg.ShowDialog();
              if (dlgResult.Equals(DialogResult.OK))
              {
                  foreach (string file in System.IO.Directory.GetFiles(folderBrowserDlg.SelectedPath, "*.png")) //.png, bmp, etc.
                  {
                      Image image = new Bitmap(file);
                      imageList1.Images.Add(image);                 
                  }
              }
      
              this.listView1.View = View.LargeIcon;
              this.imageList1.ImageSize = new Size(32, 32);
              this.listView1.LargeImageList = this.imageList1;
              for (int j = 0; j < this.imageList1.Images.Count; j++)
              {
                  ListViewItem item = new ListViewItem();
                  item.ImageIndex = j;
                  this.listView1.Items.Add(item);
              }
      

      以上代码将获取所选文件夹中存在的图像文件列表并将它们添加到列表视图控件中。

      【讨论】:

        猜你喜欢
        • 2017-05-11
        • 1970-01-01
        • 2014-01-15
        • 2014-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        相关资源
        最近更新 更多