【问题标题】:how to select multiple files without using openfileDialog in c#如何在c#中不使用openfileDialog选择多个文件
【发布时间】:2018-03-11 12:39:42
【问题描述】:

我正在制作一个简单的应用程序,它将在 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);

                }
            }

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

【问题讨论】:

    标签: c# winforms file directory openfiledialog


    【解决方案1】:

    考虑改变:

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

    到:

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

    然后你的以后:

    foreach (string img in files)
    

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

    "Cards" 作为参数传入只是有一些危险,根据the docs

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

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

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2011-01-15
      • 2012-07-22
      • 2012-07-22
      • 2017-10-19
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      相关资源
      最近更新 更多