Files in Visual Basic

The FolderBrowserDialogBrowse Folder control lets us select a directory at run time instead of hard coding in a folder such as "c:/mypictures".

  1. Continue with the project called directory.
  2. Drag FolderBrowserDialogBrowse Folder control to the form. It will not appear on the form, but under the form in design view.
  3. The control will be named FolderBrowserDialog1, we will leave it as that.
  4. 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
.The browseFolder Dialog opens
After selecting a folder, you will see a list of all the files in the folder you selected:
Selected files

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

NEXT: Display the Picture