The FolderBrowserDialog
control lets us select a directory at run time instead of hard coding in a folder such as "c:/mypictures".
- Continue with the project called directory.
- Drag FolderBrowserDialog
control to the form. It will not appear on the form, but under the form in design view.
- The control will be named FolderBrowserDialog1, we will leave it as that.
- Modify the code in form load as shown below:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FolderBrowserDialog1.ShowDialog()
Me.Text = FolderBrowserDialog1.SelectedPath
Dim Folder As String = FolderBrowserDialog1.SelectedPath & "/"
Dim S As String
S = Dir(Folder) 'get first file from Selected Folder
While S <> ""
LstFiles.Items.Add(S)
S = Dir() 'get next file in same directory
End While
End Sub
When you run the program the browser dialog opens
.
After selecting a folder, you will see a list of all the files in the folder you selected:

We display the selected path in the text. Notice that the text does not include a / at the end. We had to add a slash at the end in order for the Dir command to work:
Dim Folder As String = FolderBrowserDialog1.SelectedPath & "/"
Next: Display the picture