如果文件夹不共享相同的基本文件夹,如何检索图像

How to retrieve image if folders don't share the same base folder

我是 C# 编程语言的新手。任何人都可以帮助我如何从 folder>folder>folder>image 中的多个文件夹中检索图像。下面是我已经尝试过的代码,但它仅在 folder>image 时才检索图像。我已经试过了 string baseFolder = @"\\egmnas01\hr\photo\~"; 但还是不行。请有人帮助我。谢谢。

    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");
    }

相信以下内容对您有所帮助

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");
    }
}

请注意,此论坛中已经有类似的 questions 问题(并已回答),也许是针对不同的文件(xml 而不是 jpg) 另外,初次使用.Net APIs或遇到困惑时,请参考MSDN