扩展OpenFileDialog以同时选择文件夹和文件

时间:2012-12-06 06:55:54

标签: c# openfiledialog

我想要实现的是从OpenFileDialog中选择多个文件夹和文件同时
建议的解决方案:

OpenFileDialog.Multiselect = true;
OpenFileDialog.ValidateNames = false;
OpenFileDialog.CheckFileExists = false;
OpenFileDialog.CheckPathExists = true;
OpenFileDialog.FileName = "Dummy";
OpenFileDialog.Filter = string.Empty;

当用户选择文件夹和文件同时时,此设置无效。

我正在寻找一个解决方案,当用户按下打开按钮并获取所选文件和文件夹的路径时,我可以通过该解决方案关闭对话框。

1 个答案:

答案 0 :(得分:0)

我认为这个问题已经回答here

我会重复一遍:

如果使用FileNames属性而不是FileName属性,则会获得每个文件的字符串数组,您可以使用shift键选择多个文件。像这样:

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog x = new OpenFileDialog();
    x.Multiselect = true;
    x.ShowDialog();
    string[] result = x.FileNames;

    foreach (string y in result)
       MessageBox.Show(y, "Selected Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

对于文件和文件夹,您需要使用WinAPI中包含的CommonOpenFileDialog,特定类为here

相关问题