【发布时间】:2018-08-29 14:49:44
【问题描述】:
我正在尝试制作一个在特定文件夹(例如:CR7)而不是所有目录中查找所有图像的应用程序。我有一个网络共享和很多目录,我可以在其中找到特定文件夹“CR7”。我只需要该 CR7 文件夹中的图像。我可以找到它们并尝试将这些结果放在 datadridview 上,但没有成功。任何想法为什么filesList 不去数据表?
代码如下:
{
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();
//Find files on a specific folder (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);
}
// 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(Path.GetFileName(entryFiles), entryFiles);
}
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Visible = false;
var formToOpen = new FormResultadosFotos(tableWithPhotos);
formToOpen.Show();
}
}
}
【问题讨论】:
-
应该是:
tableWithPhotos.Rows.Add(row);为什么表单没有显示我们不知道的数据。您也必须发布该代码。