查找不同目录中特定文件夹中的文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了查找不同目录中特定文件夹中的文件相关的知识,希望对你有一定的参考价值。
我需要在不同的目录中找到特定文件夹(例如CR7)中的大量文件(图像)。想象一下,我有一个网络共享,我必须找到并显示该特定文件夹中的所有图像(CR7)。 CR7文件夹可以在以下不同的地方找到:\sharefolder01CR7
或:\sharefolder01folder02CR7
或其他任何东西。我有这个,但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 = @"\serverfolder01";
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();
}
}
答案
我认为你必须通过以下步骤拆分执行:
- 通过将
"CR7"
指定为"CR7"
,获取指定文件夹下名为searchPattern
的所有目录。 - 现在,您拥有所有CR7文件夹路径,在每个CR7目录中迭代这些集合和获取文件列表并根据扩展名列表对其进行过滤。
你可以试试这个,让我知道它是否解决了你的问题:
string allDir = @"\sharefolder01";
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);
}
以上是关于查找不同目录中特定文件夹中的文件的主要内容,如果未能解决你的问题,请参考以下文章
浏览pdf文件以查找特定页面并使用python从图像中提取表格数据