Md5 Software
Md5 Software

Reputation: 62

How to read files in folders?

I am trying to get my application to check for folders in the folderbrowserdialogs selectedpath and then get those files, but it doesn't work I have tried both listed ways below. The second way gives me an error: (Expression is of type char which is not a collection type)

    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

Any help is appreciated.

Upvotes: 1

Views: 371

Answers (1)

FolderBrowserDialog1.SelectedPath will return the path the user selected in the dialog. You still need to write code to go get the files. There may not be a need to get the folders and then files in them. Net has ways to do that for you:

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)

You also are not going to be able to simply assign the results to a ReadOnlyCollection like that, because they are ReadOnly. The collection needs to be created/instanced with the complete list:

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

Upvotes: 1

Related Questions