我需要在不同目录的特定文件夹(例如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();
}
}
答案 0 :(得分:1)
我认为您必须按以下步骤划分执行范围:
"CR7"
指定为"CR7"
,获得指定文件夹下名称为searchPattern
的所有目录。您可以尝试一下,让我知道它是否解决了您的问题:
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);
}