C# - 如何自定义 OpenFileDialog 以选择多个文件夹和文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11628021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - How to customize OpenFileDialog to select multiple folders and files?
提问by Yun
I have posted - How to use OpenFileDialog to select a folder?, I couldn't find the correct answer. So, I have changed my question.
我已发布 -如何使用 OpenFileDialog 选择文件夹?,我找不到正确答案。所以,我改变了我的问题。
I want to customize OpenFileDialog to select multiple folders and files. I tried to find a solution and could see some posts about it.
我想自定义 OpenFileDialog 以选择多个文件夹和文件。我试图找到一个解决方案,可以看到一些关于它的帖子。
From the internet, I found the following project - https://github.com/scottwis/OpenFileOrFolderDialog.
从互联网上,我找到了以下项目 - https://github.com/scottwis/OpenFileOrFolderDialog。
However, while using this, I faced one problem. It uses the GetOpenFileNamefunction and OPENFILENAMEstructure from MFC. And OPENFILENAMEhas the member named "templateID". It's the identifier for dialog template. And the sample project has the "res1.rc" file and, also have the templated dialog in it.
但是,在使用它时,我遇到了一个问题。它使用MFC的GetOpenFileName函数和OPENFILENAME结构。并且OPENFILENAME具有名为“ templateID”的成员。它是对话框模板的标识符。示例项目有“ res1.rc”文件,其中也有模板化的对话框。
But I don't know How can I attach this file to my C# project?
但我不知道如何将此文件附加到我的 C# 项目?
Or is there any other perfect solution about - "How to customize OpenFileDialog to select multiple folders and files?"?
或者有没有其他完美的解决方案 - “如何自定义 OpenFileDialog 以选择多个文件夹和文件?”?
采纳答案by JMK
If you use the FileNames property instead of the FileName property, you get a string array of each file selected, you select multiple files using the shift key. Like so:
如果您使用 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);
}
For files and folders you need to use the CommonOpenFileDialogincluded with the WinAPI, the particular class is here.
回答by Embedd_Khurja
Try this:
尝试这个:
openFileDialog.Multiselect = true;
回答by Arif Eqbal
You might not get a built in .Net control like that. Definitely the OpenFileDialog can not function as both File as well as Folder browser. You have two choices go for a third party tool like the one you found second make your own control. Surprisingly you might not find creating a very simple version of your own control very difficult.
您可能不会像那样获得内置的 .Net 控件。肯定 OpenFileDialog 不能同时用作文件和文件夹浏览器。您有两种选择,可以选择第三方工具,例如您找到的第二种工具可以自己控制。令人惊讶的是,您可能不会发现创建自己的控件的非常简单的版本非常困难。

