0

i'm a newbie in C# programming language. Can anybody help me on how to retrieve image from multiple folder which image at folder>folder>folder>image. Below is code that I already try but it only retrieve image if folder>image. I have try like this one string baseFolder = @"\\\\egmnas01\\hr\\photo\\~";but still not work. Please somebody help me. Thanks.

    string baseFolder = @"\\\\egmnas01\\hr\\photo\\";
    string[] employeeFolders = Directory.GetDirectories(baseFolder);

    string imgName = textBoxEmplNo.Text +  ".jpg";
    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 (!fileFound)
    {

      pictureBox1.Visible = true;
      pictureBox1.Image = Image.FromFile(@"C:\Users\jun\Desktop\images\photo\No-image-found.jpg");
    }
Miza
  • 49
  • 1
  • 1
  • 8
  • Do you men that you want to find any file whose name matches the filename you provide within that tree of subdirectories? You might want something like this to find the matching file(s) if you don't know the subdirectory: `System.IO.Directory.GetFiles(@"c:\test\", "\\*.jpg", System.IO.SearchOption.AllDirectories);` – ProgrammingLlama May 30 '17 at 00:58
  • @john that code replace this code `string baseFolder = @"\\\\egmnas01\\hr\\photo\\";` ? because I already try but still not work. – Miza May 30 '17 at 01:29

1 Answers1

1

I believe following should help you

static void Main(string[] args)
{
    // test path... replace with the path you need
    string baseFolder = @"D:\test\";

    string imgName = textBoxEmplNo.Text + ".jpg";
    bool fileFound = false;

    DirectoryInfo di = new DirectoryInfo(baseFolder);
    foreach (var file in di.GetFiles(imgName, SearchOption.AllDirectories))
    {
        pictureBox1.Visible = true;
        pictureBox1.Image = Image.FromFile(file.FullName);

        fileFound = true;
        break;
    }

    if (!fileFound)
    {

        pictureBox1.Visible = true;
        pictureBox1.Image = Image.FromFile(@"C:\Users\jun\Desktop\images\photo\No-image-found.jpg");
    }
}

Please note there are similar questions already asked (and answered) in this forum, perhaps, for a different file (xml instead of jpg) Also, please refer to MSDN when you start using the .Net APIs for the first time or when you are confused.

Shankar
  • 176
  • 7
  • thanks for your help. but still cannot retrieve the image. :( – Miza May 30 '17 at 01:48
  • @Miza Is it that there is a file with matching name but still the above logic is not finding it? Please print out all the filenames retrieved by `di.GetFiles(...)` to a console/message box. Do you see if it is at least listing some of the files if `di.GetFiles("*.jpg", SearchOption.AllDriectories)` call is made. You can try to write similar sample with similar nested folder structure and see where it fails, if it fails. – Shankar May 30 '17 at 01:58
  • it is necessary `static void Main(string[] args)`? because i'm code in `private void textBoxWorkNo_KeyUp(object sender, KeyEventArgs e)`. – Miza May 30 '17 at 02:34
  • The code you would be interested is within static void Main(...). I wrote a console application to communicate the intent (my answer). You may ignore static void Main(...), consider only the logic inside. – Shankar May 30 '17 at 02:37
  • Thank you very much! Finally it work successfully after try harder :). Thanks! – Miza May 30 '17 at 02:55
  • There can be various reasons why the search is slow. You may measure the time consumed to search, time consumed to load the image (assuming image size is huge). Use [StopWatch](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch(v=vs.110).aspx) then perhaps you can focus on the root cause of the slowness. – Shankar May 30 '17 at 04:19
  • Thanks for your respond. I'm still trying. – Miza May 30 '17 at 07:57