VB.NET FolderBrowserDialog – Pick Folders Easily (2026)
The VB.NET FolderBrowserDialog lets users pick a folder from the file system before your application processes its contents. It displays the standard Windows folder picker, returns the selected path, and optionally restricts the browsing root. This guide covers setup, root folder configuration, new-folder creation, batch processing, and common pitfalls.
Inhaltsverzeichnis
- 1 What is the FolderBrowserDialog?
- 2 Basic example
- 3 Setting the root folder
- 4 Pre-selecting a folder
- 5 Hiding the New Folder button
- 6 Complete example: batch-processing files
- 7 Including subfolders
- 8 Using Environment.SpecialFolder
- 9 All important properties at a glance
- 10 Common mistakes
- 11 FAQ
- 12 Wrapping up
What is the FolderBrowserDialog?
The FolderBrowserDialog is a WinForms control from System.Windows.Forms. When you call ShowDialog(), it opens the Windows folder picker. The user navigates the folder tree, selects a folder, and clicks OK. Your code then reads the full folder path from the SelectedPath property.
Use it when:
- The user should choose an output folder for exports, backups, or reports
- Your application needs to batch-process all files inside a directory
- The user should pick an installation or working directory
- You want to let the user create a new folder on the fly
If the user should pick a single file instead, use the OpenFileDialog. To save a file to a specific location, use the SaveFileDialog.
Basic example
The simplest FolderBrowserDialog that displays the selected path:
Using dlg As New FolderBrowserDialog()
dlg.Description = "Select a folder"
If dlg.ShowDialog() = DialogResult.OK Then
MessageBox.Show($"Selected: {dlg.SelectedPath}")
End If
End Using
The Using block disposes the dialog after use. ShowDialog() returns DialogResult.OK when the user confirms, or DialogResult.Cancel when they close the dialog. Always check the result before using the path.
Setting the root folder
The RootFolder property restricts which top-level node the dialog shows. It accepts values from Environment.SpecialFolder:
Using dlg As New FolderBrowserDialog()
' Only show folders inside Desktop
dlg.RootFolder = Environment.SpecialFolder.Desktop
dlg.Description = "Select a project folder"
If dlg.ShowDialog() = DialogResult.OK Then
Dim folder As String = dlg.SelectedPath
End If
End Using
Common values for RootFolder:
Environment.SpecialFolder.Desktop(default) shows the full tree starting from DesktopEnvironment.SpecialFolder.MyComputershows all drivesEnvironment.SpecialFolder.MyDocumentslimits browsing to the Documents folder
Pre-selecting a folder
Use SelectedPath to pre-select a folder when the dialog opens. This is useful when the user already chose a folder earlier and you want to remember it:
Using dlg As New FolderBrowserDialog()
dlg.Description = "Select export folder"
dlg.SelectedPath = "C:\Users\Public\Documents"
If dlg.ShowDialog() = DialogResult.OK Then
Dim exportFolder As String = dlg.SelectedPath
End If
End Using
The dialog opens with that folder already highlighted. If the path does not exist, the dialog falls back to the root.
By default, the dialog includes a „Make New Folder“ button. If the user should only pick an existing folder, disable it:
Using dlg As New FolderBrowserDialog()
dlg.Description = "Select an existing folder"
dlg.ShowNewFolderButton = False
If dlg.ShowDialog() = DialogResult.OK Then
Dim folder As String = dlg.SelectedPath
End If
End Using
Set ShowNewFolderButton = False for read-only operations like importing files. Keep it True (the default) when the user might need to create a destination folder for exports.
Complete example: batch-processing files
A realistic example that lets the user pick a folder and then processes all text files inside it:
Imports System.IO
Imports System.Text
Private Sub BtnImport_Click(sender As Object, e As EventArgs) Handles BtnImport.Click
Using dlg As New FolderBrowserDialog()
dlg.Description = "Select folder with text files"
dlg.ShowNewFolderButton = False
If dlg.ShowDialog() = DialogResult.OK Then
Dim files() As String = Directory.GetFiles(
dlg.SelectedPath, "*.txt")
For Each filePath In files
Dim content As String = File.ReadAllText(
filePath, Encoding.UTF8)
' Process each file...
Next
MessageBox.Show($"{files.Length} files processed.")
End If
End Using
End Sub
Directory.GetFiles() returns all matching files in the selected folder. Use the second parameter for pattern matching, e.g. "*.csv" or "*.xml". For more on reading text files, see the VB.NET Read Text File guide.
Including subfolders
To process files recursively, add SearchOption.AllDirectories:
If dlg.ShowDialog() = DialogResult.OK Then
Dim files() As String = Directory.GetFiles(
dlg.SelectedPath, "*.txt", SearchOption.AllDirectories)
For Each filePath In files
Dim relativePath As String = filePath.Replace(
dlg.SelectedPath & "\", "")
' Process file with relative path info...
Next
End If
This searches the selected folder and all its subfolders. Be careful with large directory trees, as this can take a while if there are thousands of files.
Using Environment.SpecialFolder
You can combine the dialog with Environment.GetFolderPath() to pre-select common system folders. You can also use the application path as the starting point:
' Start in Documents
Using dlg As New FolderBrowserDialog()
dlg.SelectedPath = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments)
If dlg.ShowDialog() = DialogResult.OK Then
' Use dlg.SelectedPath...
End If
End Using
' Start in the application directory
Using dlg As New FolderBrowserDialog()
dlg.SelectedPath = Application.StartupPath
If dlg.ShowDialog() = DialogResult.OK Then
' Use dlg.SelectedPath...
End If
End Using
All important properties at a glance
| Property | Default | Purpose |
|---|---|---|
Description | (empty) | Text shown above the folder tree |
SelectedPath | (empty) | Pre-selected folder / selected folder after dialog |
RootFolder | Desktop | Top-level node of the folder tree |
ShowNewFolderButton | True | Show or hide the „Make New Folder“ button |
The FolderBrowserDialog has fewer properties than the OpenFileDialog because it only deals with folders, not files. There is no Filter or Multiselect property.
Common mistakes
Using the path without checking DialogResult
' BAD - SelectedPath is empty if user cancels
dlg.ShowDialog()
Dim files = Directory.GetFiles(dlg.SelectedPath)
' GOOD - check the result first
If dlg.ShowDialog() = DialogResult.OK Then
Dim files = Directory.GetFiles(dlg.SelectedPath)
End If
Confusing RootFolder and SelectedPath
RootFolder limits which folders the user can see. SelectedPath pre-selects a folder within the visible tree. If you set RootFolder = MyDocuments, the user cannot navigate outside of Documents. If you only want to suggest a starting point but allow full navigation, use SelectedPath instead.
Forgetting the Using block
Like all WinForms dialogs, the FolderBrowserDialog holds unmanaged resources. Without Using, those resources stay in memory until the garbage collector runs. Always wrap it in a Using block.
FAQ
Create a New FolderBrowserDialog() inside a Using block, optionally set Description, then call ShowDialog(). Check if the result equals DialogResult.OK before reading dlg.SelectedPath.
Set dlg.SelectedPath to the desired folder path before calling ShowDialog(). The dialog opens with that folder highlighted. Do not confuse this with RootFolder, which restricts the visible tree.
Yes. Set dlg.ShowNewFolderButton = False before calling ShowDialog(). This is useful for read-only operations where the user should only select an existing folder.
RootFolder limits the top-level node the user can browse, restricting navigation. SelectedPath pre-selects a folder within the visible tree without restricting navigation. Use SelectedPath when you want a starting point but full access.
No. The built-in FolderBrowserDialog only supports selecting a single folder. If you need multiple folder selection, you would need to call the dialog multiple times or use a custom solution.
Wrapping up
The FolderBrowserDialog gives your VB.NET application a native folder picker with minimal code. Set SelectedPath to suggest a starting folder, use RootFolder to restrict navigation, and hide the New Folder button with ShowNewFolderButton = False when the user should only select existing folders. After the dialog closes, combine SelectedPath with Directory.GetFiles() to process folder contents. For picking individual files, use the OpenFileDialog. To save files, see the SaveFileDialog.