The
DirectoryInfo.GetFiles
method finds all matching files and loads them into an array before returning control to your code. Your code then reads the first matching file, and ignores the rest.
Try using
EnumerateFiles
instead; that returns each file as it's found, without waiting for the search to complete:
bool fileFound = false;
DirectoryInfo di = new DirectoryInfo(baseFolder);
foreach (var file in di.EnumerateFiles(imgName, SearchOption.AllDirectories))
{
pictureBox1.Visible = true;
pictureBox1.Image = Image.FromFile(file.FullName);
fileFound = true;
break;
}
if (!fileFound)
{
...
}
You could even use LINQ to simplify that:
DirectoryInfo di = new DirectoryInfo(baseFolder);
FileInfo file = di.EnumerateFiles(imgName, SearchOption.AllDirectories).FirstOrDefault();
if (file != null)
{
pictureBox1.Visible = true;
pictureBox1.Image = Image.FromFile(file.FullName);
}
else
{
...
}