打开文件对话框多选 [英] Openfiledialog Multiselect

查看:91
本文介绍了打开文件对话框多选的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多选文件对话框浏览多张图片并将其添加到datagridview中,然后从那里将其存储在数据库中.

I am using a multiselect file dialog to browse multiple pictures and add them to a datagridview then from there store them in the database.

我只设法存储了第一张选定的图片,这是有问题的(根本没有语法或运行时错误).

Something was wrong in term of that I only managed to store the first selected picture (No syntax or runtime errors at all).

经检查,我意识到文件对话框仅获取第一张图像的完整路径,并将其用于其余图像.

Upon inspection I've realized that the file dialog gets the full path of the first image only and uses it for the rest of images.

示例代码:

if (ofd_pic.ShowDialog() == DialogResult.OK)
{
   foreach (String file in ofd_pic.FileNames)
   {
    MessageBox.Show(ofd_pic.FileName);
   }
}

该消息框将始终仅显示第一张图像的路径,而我无法获取每个选定图像的路径.

That messagebox will always show the path of the first image only and I wasn't able to get the path of every single selected image.

文件对话框的属性为:

1.修饰符:私有. 2. MultiSelect:正确. 3. RestoreDirectory:正确.

1.Modifiers: Private. 2. MultiSelect: True. 3. RestoreDirectory: True.

有帮助吗?

推荐答案

您实际上是在遍历所有文件,但从未使用过它.您需要使用循环变量file

You're actually looping through all the files, but you never use it. You need to use the loop variable file

foreach (String file in ofd_pic.FileNames)
{
    MessageBox.Show(file);
}

仅当将MultiSelect设置为false时,才应使用

ofd_pic.FileName属性,这才有意义.我猜想FileName启用MultiSelect后会返回第一个文件.

ofd_pic.FileName property should be used only when you set MultiSelect to false, then only it makes sense. I guess FileName returns the first file when you have enabled MultiSelect.

这篇关于打开文件对话框多选的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆