简体   繁体   中英

how to select multiple files without using openfileDialog in c#

I am making a simple app which will show the images from a specific folder on a winform load. I've done the "showing part" with using openfileDialog and it's working fine, where the user has to select images each time.I want to make it more automatic that user doesn't have to select the files, It should select the files automatically.

Target Folder is static(It'll remain same) while it contains multiple images. How can I obtain all of the image files from the directory?

I am using this code.

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

                }
            }

but stuck at string[] files = filepath; here. Please guide me where I am making any mistake. Please let me know if anybody needs more info. Thanks in advance.

Consider changing:

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

to:

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

Then your later:

foreach (string img in files)

will iterate over all file in the Cards folder.

There are some dangers in passing in just "Cards" as a parameter, as per the docs :

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

It would be better, if possible, to pass an absolute path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM