我正在编写一个小程序,用户将在其中选择一组文件(主要是.CSV
)文件,我的程序将搜索它们并查找所需的数据。
它可以处理多达300个文件,但之后它无法正常工作。它给了我一个错误:
InvalidOperationExceprion was Unhandaled
Too Many files are selected. Select Fewer files and try again.
我该怎么办?
答案 0 :(得分:0)
就像Eric Walker所说,打开文件对话框可以处理1,000个和1,000个文件,没有理由它会停在300个。
循环文件/文件夹以获取信息的最佳方法是使用.GetFiles()方法
Dim OFD As New FolderBrowserDialog
If OFD.ShowDialog = DialogResult.OK Then
For Each f In Directory.GetFiles(OFD.SelectedPath)
If Path.GetExtension(f) = ".txt" Path.GetExtension(f) = ".csv" Then
Dim reader As String() = File.ReadAllLines(f)
For each line as String in reader
DoSomethingAwesome(line)
Next
End If
Next
End If
这将循环显示某个目录中的每个文件。
现在,如果您想循环浏览文件对话框中的每个文件,那么您可以使用它。
Dim OFD As New OpenFileDialog()
OFD.Multiselect = True
If OFD.ShowDialog = DialogResult.OK Then
For Each f In OFD.FileNames
If Path.GetExtension(f) = ".txt" Path.GetExtension(f) = ".csv" Then
Dim reader As String() = File.ReadAllLines(f)
For Each line As String In reader
DoSomethingAwesome(line)
Next
End If
Next
End If
根据您的喜好尝试其中一种。
作为附注,对于将来的帖子 - 请发布您尝试过的代码或更多有关您要完成的内容以及遇到问题的详细信息。坦率地说,我很惊讶你没有被投票(非常惊讶,SO上的人可能是无情的)。只是一个友好的提示。