简体   繁体   English

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

[英]Finding files in specific folder in different directories

I need to find a lot of files (images) in a specific folder (for exemple CR7), in different directories.我需要在不同目录的特定文件夹(例如 CR7)中找到很多文件(图像)。 Imagine I have a network share that I have to find and display all images in that specific folder (CR7).想象一下,我有一个网络共享,我必须在该特定文件夹 (CR7) 中查找和显示所有图像。 CR7 folder can be found in different places like: \\\\share\\folder01\\CR7 or: \\\\share\\folder01\\folder02\\CR7 or anything else. CR7 文件夹可以在不同的地方找到,例如: \\\\share\\folder01\\CR7或: \\\\share\\folder01\\folder02\\CR7或其他任何地方。 What I have is this, but results from filesList dont go to datagridview:我所拥有的是这个,但是来自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();
    }
}

I think you have to split the execution by following steps:我认为您必须通过以下步骤拆分执行:

  • Get all Directories with name "CR7" under the specified folder by specifying "CR7" as searchPattern .通过将"CR7"指定为searchPattern来获取指定文件夹下名称为"CR7"所有目录。
  • Now you have all CR7 folder paths, Iterate through those collection and Get List of files and filter them based on the extension list, in each CR7 directory.现在您拥有所有 CR7 文件夹路径,遍历这些集合并获取文件列表,并根据每个 CR7 目录中的扩展名列表过滤它们。

Can you please try this and let me know whether it solved your issues:你能试试这个吗,让我知道它是否解决了你的问题:

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

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

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