0

How can I look for an image in multiple folders? This is my current code but it only retrieves from one folder.

string imgFilePath = @"C:\Users\may\Documents\Visual Studio 2013\WebSites\EV\photo\" 
                      + textBoxEmplNo.Text + ".jpg";
if (File.Exists(imgFilePath))
{
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(imgFilePath);
}
else
{
    //Display message that No such image found
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(@"C:\Users\may\Documents\Visual Studio 2013\WebSites\EV\photo\No-image-found.jpg");
}
EpicKip
  • 4,015
  • 1
  • 20
  • 37
Miza
  • 49
  • 1
  • 1
  • 8
  • 1
    Retrieve how? From 1 textbox? at random? Just pick a path? You have to explain your question – EpicKip May 09 '17 at 07:53
  • http://stackoverflow.com/questions/11628021/c-sharp-how-to-customize-openfiledialog-to-select-multiple-folders-and-files – Rachel Gallen May 09 '17 at 07:55
  • @EpicKip yes, search image from 1 textbox. From that textbox, it check from what file the image have, then display the image at picturebox. – Miza May 09 '17 at 08:02
  • And how are you going to search multiple files from 1 textbox? What do i have to type in there to get what files? You have to explain at least how you want it to work – EpicKip May 09 '17 at 08:04
  • @EpicKip I have 1000+ image of employee from three file. Each file contains 300 image. When user enter value at textbox employee no., the image of employee should display at picturebox. I do not know how to display the image at picturebox when I have three file. How to read from each file to display image in picturebox? – Miza May 09 '17 at 08:13
  • Each file.. so is it a zip file or something? Because usually `1 file == 300 files` returns `false` – EpicKip May 09 '17 at 08:14
  • @EpicKip sorry. from folder :) – Miza May 09 '17 at 08:18
  • So in each folder there is 1 file with the specified name? – EpicKip May 09 '17 at 08:31
  • If that's the case I suggest making an array with the folders to look in. Would be quite easy to accomplish :P – EpicKip May 09 '17 at 08:32
  • Oh and does 1 employee have multiple images? Lets say id.1 has an image in folder 1,2 and 3 how would you want to display 3 picturebox's – EpicKip May 09 '17 at 08:37
  • @EpicKip Yes, each folder there is 1 file with the specified name :) – Miza May 09 '17 at 09:01
  • @Miza How are you going to display multiple images in a picturebox? or do you want multiple picturebox's or a listbox where you can pick? – EpicKip May 09 '17 at 09:02
  • @Miza Ill come back once you edited your question so it actually says what you want. You have to think about these things, 1 picturebox is for 1 picture. You want 3, how will you display it? – EpicKip May 09 '17 at 09:07
  • @EpicKip Thanks for your respond. I already update my question. I want to retrieve image from many folder. User can search employee image from employee no. textbox. Then the image will display at picturebox. My problem is I do not know how to retrieve image from many folder. FYI, images is name same as employee no. – Miza May 11 '17 at 00:46
  • @Miza Added answer – EpicKip May 12 '17 at 06:37

2 Answers2

1

I changed your code just a bit to search for an image in multiple folders:

//BaseFolder that contains the multiple folders 
string baseFolder = "C:\\Users\\myName\\Desktop\\folderContainingFolders";
string[] employeeFolders = Directory.GetDirectories( baseFolder );

//Hardcoded employeeNr for test and png because I had one laying around
string imgName =  "1.png";

//Bool to see if file is found after checking all
bool fileFound = false;

foreach ( var folderName in employeeFolders )
{
    var path = Path.Combine(folderName, imgName);
    if (File.Exists(path))
    {
        pictureBox1.Visible = true;
        pictureBox1.Image = Image.FromFile(path);
        fileFound = true;
        //If you want to stop looking, break; here 
    }
}
if(!fileFound)
{
    //Display message that No such image found
    pictureBox1.Visible = true;
    pictureBox1.Image = Image.FromFile(@"C:\Users\may\Documents\Visual Studio 2013\WebSites\EV\photo\No-image-found.jpg");
}
EpicKip
  • 4,015
  • 1
  • 20
  • 37
  • @Miza No problem glad I could help – EpicKip May 15 '17 at 06:43
  • do you mind if I ask question? How can I retrieve from one folder that have multiple folder? That means **string[] employeeFolders = { "folderNameA", "folderNameB", "folderNameC" };** is no need. Because I don't want to adjust the code again when added folder. Thanks – Miza May 15 '17 at 08:14
  • @Miza You mean loop through all folders in a certain "base" folder? – EpicKip May 15 '17 at 08:15
  • I can post code that can fill the array for you by base folder? – EpicKip May 15 '17 at 08:17
  • Can you edit in your previous code? because I tried but still not work. – Miza May 15 '17 at 08:37
0

try code snippet given below. you can add n number of folder path to look at by just adding new entry into folder path string array.

String[] possibleFolderPaths = new String[] { "folderpath1" , "folderpath2", "folderpath3" };
        String imgExtension = ".jpg";
        Boolean fileFound = false;
        string imgFilePath = String.Empty;

        // loop through each folder path to check if file exists in it or 
not.
        foreach (String folderpath in possibleFolderPaths)
        {
            imgFilePath = String.Concat(folderpath, 
textBoxEmplNo.Text.Trim(),imgExtension);

            fileFound = File.Exists(imgFilePath);

            // break if file found.
            if (fileFound)
            {
                break;
            }

        }

        if (fileFound)
        {
            pictureBox1.Visible = true;
            pictureBox1.Image = Image.FromFile(imgFilePath);

            imgFilePath = string.Empty;
        }
        else
        {
            //Display message that No such image found
            pictureBox1.Visible = true;
            pictureBox1.Image = Image.FromFile(@"C:\Users\may\Documents\Visual Studio 2013\WebSites\EV\photo\No-image-found.jpg");
        }