如何读取文件夹中的文件?

How to read files in folders?

我试图让我的应用程序检查 folderbrowserdialogs selectedpath 中的文件夹,然后获取这些文件,但它不起作用我已经尝试了下面列出的两种方法。第二种方法给我一个错误:(表达式的类型 char 不是集合类型)

    For Each folder In FileBrowserDialog.SelectedPath
        Dim counter As  _
System.Collections.ObjectModel.ReadOnlyCollection(Of String)
            counter = My.Computer.FileSystem.GetFiles(folder)
            Label1.Text = counter.Count.ToString
        Next



For Each folder In FileBrowserDialog.SelectedPath
        Dim counter As  _
System.Collections.ObjectModel.ReadOnlyCollection(Of String)
For Each foundfile In folder
            counter = My.Computer.FileSystem.GetFiles(foundfile)
            Label1.Text = counter.Count.ToString
        Next

感谢任何帮助。

FolderBrowserDialog1.SelectedPath 将 return 用户在对话框中选择的路径。您仍然需要编写代码来获取文件。可能不需要获取文件夹然后获取其中的文件。 Net 有办法为您做到这一点:

FolderBrowserDialog1.ShowDialog()
Dim myPath As String = FolderBrowserDialog1.SelectedPath

' get all files for a folder
Dim files = Directory.GetFiles(myPath)

' get all files for all sub folders
Dim files = Directory.GetFiles(myPath, "*.*",
         System.IO.SearchOption.AllDirectories)

' get certain file types for folder and subs
Dim files = Directory.GetFiles(myPath, "*.jpg",
         System.IO.SearchOption.AllDirectories)

您也不能像那样简单地将结果分配给 ReadOnlyCollection,因为它们是只读的。集合需要 created/instanced 完整列表:

Dim counter As new ReadOnlyCollection(Of String)(files)