C# 如何在c中不使用openfileDialog选择多个文件

C# 如何在c中不使用openfileDialog选择多个文件,c#,winforms,file,directory,openfiledialog,C#,Winforms,File,Directory,Openfiledialog,我正在制作一个简单的应用程序,它将在winform加载时显示特定文件夹中的图像。 我已经使用openfileDialog完成了显示部分,它工作正常,用户每次都必须选择图像。我想让它更自动化,用户不必选择文件,它应该自动选择文件 目标文件夹是静态的,当它包含多个图像时将保持不变。 如何从目录中获取所有图像文件 我正在使用这个代码 string[] path = Directory.GetFiles("Cards"); foreach (string filepath in

我正在制作一个简单的应用程序,它将在winform加载时显示特定文件夹中的图像。 我已经使用openfileDialog完成了显示部分,它工作正常,用户每次都必须选择图像。我想让它更自动化,用户不必选择文件,它应该自动选择文件

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

我正在使用这个代码

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

                }
            }
但是在字符串[]files=filepath处卡住了;在这里 请告诉我哪里出错。 如果有人需要更多信息,请告诉我。 提前感谢。

考虑更改:

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

然后,您的稍后:

foreach (string img in files)
将迭代Cards文件夹中的所有文件

仅将卡片作为参数传递存在一些危险,如下所示:

相对路径信息被解释为相对于当前路径 工作目录

如果可能,最好通过绝对路径。

考虑更改:

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

然后,您的稍后:

foreach (string img in files)
将迭代Cards文件夹中的所有文件

仅将卡片作为参数传递存在一些危险,如下所示:

相对路径信息被解释为相对于当前路径 工作目录

如果可能的话,最好通过一条绝对路径