如何在.NET中使用OpenFileDialog获取文件名(1000+文件多选)
也许你们中的一些人以前遇到过这个......
我正在打开文件进行解析。当然,我使用的是 OpenFileDialog,但 .FileNames 字符串的缓冲区限制为 2048。因此,我只能选择几百个文件。这对于大多数情况来说都是可以的。然而,例如,在一种情况下我有 1400 个文件要打开。您知道如何使用打开文件对话框执行此操作吗?我只想要 .FileNames 的字符串数组,我将其传递给解析器类。
我还考虑提供一个FolderBrowserDialog 选项,然后使用其他一些方法来循环访问目录中的所有文件,例如DirectoryInfo 类。如果我无法拥有一体化的解决方案,我会将其作为最后的手段。
Maybe some of you have come across this before....
I am opening files for parsing. I'm using OpenFileDialog, of course, but i'm limited to a buffer of 2048 on the .FileNames string. Thus, I can only select a few hundred files. This is OK for most cases. However, fore example, I have in one case 1400 files to open. Do you know a way to do this with the open file dialog. I just want the string array of .FileNames, I pass that to parser class.
I was also thinking of offering a FolderBrowserDialog option and then I'd use some other method to just loop through all the files in a directory, like the DirectoryInfo class. I'd do this as a last resort if I can't have an all in one solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最终做的是编写一个使用 OpenFileDialog 的方法,但间接检查路径字符串的长度。也就是说,如果该方法失败,则会向用户显示一条错误,告诉他们文件太多,然后会显示一个文件夹浏览器,其中包含用户正在查找的选定文件夹。我还添加了单独的选项来导入文件或导入菜单栏中的文件夹。
这是执行此操作的代码。这些是名为 DataFileIO 的静态类中的方法,我在其中放置了所有自定义 IO 内容以写入 excel、access 或 xml 等。
What I ended up doing was writing a method uses the OpenFileDialog, but checks for the length of the path string indirectly. That is if the method fails, an error is displayed to the user telling them that there are too many files, and then a FolderBrowser is shown with the folder selected that the user was looking in. I also added seperate options to import files or import folders in the menubar.
Here is the code to do that. These are Methods in a static class called DataFileIO where I put all the customize IO stuff for writing to excel or access or xml, etc.
天哪,我无法想象在文件打开对话框中选择 1400 个文件。也许您应该只允许用户键入过滤器,然后执行 System.IO.Directory.GetFiles 调用。
my gosh I can't imagine selecting 1400 files in a file open dialog. Perhaps you should just allow the user to key in a filter and then do a System.IO.Directory.GetFiles call.
我肯定会选择FolderBrowser 路线。我永远不想手动选择 50-100 个文件,更不用说 1000 多个文件。最好检索文件夹,提示某些模式匹配并以这种方式选择它们。从可用性的角度来看,选择大量文件恕我直言是一个糟糕的选择。
I'd definitely go the FolderBrowser route. I'd NEVER want to have to select 50-100 much less 1000+ files manually. Better to retrieve the folder, prompt for some pattern to match and select them that way. From a usability standpoint, choosing a large number of files is a bad choice IMHO.
您收到任何错误或异常吗?您确定正在使用
System.Windows.Forms
命名空间中的OpenFileDialog
吗?以下代码可以完美地处理所选的 2000 多个文件:
Do you get any error or exception? Are you certain that you are using the
OpenFileDialog
from theSystem.Windows.Forms
namespace?The following code works perfectly with more than 2000 files selected: