如何在 c# 中不使用 openfileDialog select 多个文件

how to select multiple files without using openfileDialog in c#

我正在制作一个简单的应用程序,它将在 winform 加载时显示特定文件夹中的图像。 我已经使用 openfileDialog 完成了 "showing part" 并且它工作正常,用户必须 select 每个图像 time.I 想要使其更加自动化,用户不必 select 文件,它应该自动 select 文件。

目标文件夹是静态的(它将保持不变)但它包含多个图像。 如何从目录中获取所有图像文件?

我正在使用此代码。

 string[] path = Directory.GetFiles("Cards");
            foreach (string filepath in path)
            {
                string[] files = filepath;



                int x = 20;
                int y = 20;
                int maxheight = -1;
                foreach (string img in files)
                {
                    PictureBox pic = new PictureBox();
                    pic.Image = Image.FromFile(img);
                    pic.Location = new Point(x, y);
                    pic.Size = new Size(200, 200);
                    pic.SizeMode = PictureBoxSizeMode.Zoom;
                    x += pic.Width + 10;
                    maxheight = Math.Max(pic.Height, maxheight);
                    if (x > this.ClientSize.Width - 100)
                    {
                        x = 20;
                        y += maxheight + 10;
                    }
                    this.panelImages.Controls.Add(pic);

                }
            }

但卡在了 string[] files = filepath; 这里。 请指导我在哪里犯了任何错误。 如果有人需要更多信息,请告诉我。 提前致谢。

考虑更改:

string[] path = Directory.GetFiles("Cards");
            foreach (string filepath in path)
            {
                string[] files = filepath;

至:

string[] files = Directory.GetFiles("Cards");

那你以后:

foreach (string img in files)

将遍历 Cards 文件夹中的所有文件。

根据 the docs:just "Cards" 作为参数传递存在一些危险

Relative path information is interpreted as relative to the current working directory.

如果可能的话,传递一个绝对路径会更好。