查找不同目录中特定文件夹中的文件

问题描述 投票:-3回答:1

我需要在不同的目录中找到特定文件夹(例如CR7)中的大量文件(图像)。想象一下,我有一个网络共享,我必须找到并显示该特定文件夹中的所有图像(CR7)。 CR7文件夹可以在以下不同的地方找到:\\share\folder01\CR7或:\\share\folder01\folder02\CR7或其他任何东西。我有这个,但filesList的结果不要去datagridview:

public partial class FormProcuraFotos : Form
{
    DataTable tableWithPhotos;
    public FormProcuraFotos()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        progressBar1.Visible = true;
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += this.Worker_DoWork;
        worker.RunWorkerCompleted += this.Worker_RunWorkerCompleted;
        worker.RunWorkerAsync();
    }
    private void Worker_DoWork(object sender, DoWorkEventArgs e)
    {
        // Create the new DataTable to be used
        tableWithPhotos = new DataTable();
        tableWithPhotos.Columns.Add("Filenames");
        tableWithPhotos.Columns.Add("Ctrl+C");
        //Find files on a specific folder (CR7)
        string allDir = @"\\server\folder01";
        var CR7Directories = Directory.EnumerateDirectories(allDir, "CR7", SearchOption.AllDirectories);
        List<string> extensions = new List<string>() { ".jpg", ".bmp", ".png", ".tiff", ".gif" };
        List<string> filesList = new List<string>();
        foreach (var dir in CR7Directories)
        {
            List<string> FileNames = new DirectoryInfo(dir).EnumerateFiles(dir)
                                                           .Where(x => extensions.Contains(x.Extension))
                                                           .Select(x => x.Name).ToList();
            filesList.AddRange(FileNames);
        }
        // And now here we will add all the files that it has found into the DataTable
        foreach (string entryFiles in filesList)
        {
            DataRow row = tableWithPhotos.NewRow();
            row[0] = Path.GetFileName(entryFiles);
            row[1] = entryFiles;
            tableWithPhotos.Rows.Add(row);
        }
    }
    private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressBar1.Visible = false;
        var formToOpen = new FormResultadosFotos(tableWithPhotos);
        formToOpen.Show();
    }
}
c# find directory
1个回答
1
投票

我认为你必须通过以下步骤拆分执行:

  • 通过将"CR7"指定为"CR7",获取指定文件夹下名为searchPattern的所有目录。
  • 现在,您拥有所有CR7文件夹路径,在每个CR7目录中迭代这些集合和获取文件列表并根据扩展名列表对其进行过滤。

你可以试试这个,让我知道它是否解决了你的问题:

string allDir = @"\\share\folder01";
var CR7Directories = Directory.EnumerateDirectories(allDir, "CR7", SearchOption.AllDirectories);
List<string> extensions = new List<string>() { ".jpg", ".bmp", ".png", ".tiff", ".gif" };
List<string> filesList = new List<string>();
foreach (var dir in CR7Directories)
{
    List<string> FileNames = new DirectoryInfo(dir).EnumerateFiles(dir)
                                                   .Where(x => extensions.Contains(x.Extension))
                                                   .Select(x => x.Name).ToList();
    filesList.AddRange(FileNames);
}
© www.soinside.com 2019 - 2024. All rights reserved.