VB.NET OpenFileDialog – Open Files with Dialog (2026)
The VB.NET OpenFileDialog lets users pick one or more files to open before your application reads them. It handles path selection, file type filtering, and file-exists validation in a single control. This guide covers setup, filters, multi-select, common patterns, and integration with actual file reading.
Inhaltsverzeichnis
- 1 What is the OpenFileDialog?
- 2 Basic example
- 3 Setting up file type filters
- 4 Setting the default folder
- 5 Selecting multiple files
- 6 Complete example: reading a text file
- 7 Reading binary files (images, PDFs)
- 8 Reading large files with StreamReader
- 9 All important properties at a glance
- 10 Common mistakes
- 11 FAQ
- 12 Wrapping up
What is the OpenFileDialog?
The OpenFileDialog is a WinForms control from System.Windows.Forms. When you call ShowDialog(), it opens the standard Windows „Open“ dialog. The user navigates to a folder, picks a file, and clicks Open. Your code then receives the full file path through the FileName property.
Use it when:
- The user should choose which file to open (imports, config files, images)
- You need a file type filter (e.g. only .txt, .csv, or .png)
- You want built-in validation that the selected file actually exists
- You need to let the user select multiple files at once
If you need the user to pick a save destination instead, use the SaveFileDialog.
Basic example
The simplest OpenFileDialog that reads a text file:
Using dlg As New OpenFileDialog()
dlg.Filter = "Text files (*.txt)|*.txt"
dlg.Title = "Open text file"
If dlg.ShowDialog() = DialogResult.OK Then
Dim content As String = File.ReadAllText(dlg.FileName)
TextBox1.Text = content
End If
End Using
The Using block disposes the dialog after use. ShowDialog() returns DialogResult.OK when the user clicks Open, or DialogResult.Cancel when they close the dialog. Always check the result before reading.
Setting up file type filters
The Filter property controls which file types appear in the dropdown. The syntax is identical to the SaveFileDialog:
' Single filter dlg.Filter = "Text files (*.txt)|*.txt" ' Multiple filters dlg.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv|All files (*.*)|*.*" ' Pre-select the second filter (1-based index) dlg.FilterIndex = 2 ' Allow multiple extensions in one filter dlg.Filter = "Images (*.png;*.jpg;*.gif)|*.png;*.jpg;*.gif"
To combine multiple extensions in a single filter entry, separate them with semicolons inside the pattern part: *.png;*.jpg;*.gif. The user sees one „Images“ dropdown entry that matches all three types.
Setting the default folder
Using dlg As New OpenFileDialog()
' Start in the user's Documents folder
dlg.InitialDirectory = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments)
dlg.Filter = "CSV files (*.csv)|*.csv"
If dlg.ShowDialog() = DialogResult.OK Then
Dim content As String = File.ReadAllText(dlg.FileName)
End If
End Using
Common choices for InitialDirectory:
Environment.SpecialFolder.MyDocumentsfor user documentsEnvironment.SpecialFolder.Desktopfor the desktop- The application path for files next to the executable
If you don’t set InitialDirectory, the dialog remembers the last folder the user navigated to during the current session.
Selecting multiple files
Set Multiselect = True to let the user select more than one file. The selected paths are available through FileNames (plural):
Using dlg As New OpenFileDialog()
dlg.Filter = "Images (*.png;*.jpg)|*.png;*.jpg"
dlg.Multiselect = True
dlg.Title = "Select images to import"
If dlg.ShowDialog() = DialogResult.OK Then
For Each filePath In dlg.FileNames
Dim img As Image = Image.FromFile(filePath)
' Process each image...
Next
MessageBox.Show($"{dlg.FileNames.Length} files selected.")
End If
End Using
The user holds Ctrl to pick individual files, or Shift to select a range. FileName (singular) still returns the first selected file; FileNames returns all of them as a String() array.
Complete example: reading a text file
A realistic example that reads a text file with encoding support, using ReadAllText:
Imports System.IO
Imports System.Text
Private Sub BtnOpen_Click(sender As Object, e As EventArgs) Handles BtnOpen.Click
Using dlg As New OpenFileDialog()
dlg.Title = "Open report"
dlg.Filter = "Text files (*.txt)|*.txt|CSV files (*.csv)|*.csv|All files (*.*)|*.*"
dlg.InitialDirectory = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments)
If dlg.ShowDialog() = DialogResult.OK Then
Dim content As String = File.ReadAllText(dlg.FileName, Encoding.UTF8)
TextBox1.Text = content
Me.Text = $"Editor - {Path.GetFileName(dlg.FileName)}"
End If
End Using
End Sub
Specify Encoding.UTF8 when reading text files to correctly handle special characters. Path.GetFileName() extracts just the file name from the full path, which is useful for setting the window title.
Reading binary files (images, PDFs)
OpenFileDialog works the same way for binary content. Only the read method changes:
' Load an image into a PictureBox
Using dlg As New OpenFileDialog()
dlg.Filter = "Images (*.png;*.jpg;*.bmp)|*.png;*.jpg;*.bmp"
If dlg.ShowDialog() = DialogResult.OK Then
PictureBox1.Image = Image.FromFile(dlg.FileName)
End If
End Using
' Read a file as byte array
Using dlg As New OpenFileDialog()
dlg.Filter = "All files (*.*)|*.*"
If dlg.ShowDialog() = DialogResult.OK Then
Dim bytes() As Byte = File.ReadAllBytes(dlg.FileName)
' Process bytes...
End If
End Using
Reading large files with StreamReader
For large files or line-by-line processing, use a StreamReader with the path from the dialog:
Using dlg As New OpenFileDialog()
dlg.Filter = "CSV files (*.csv)|*.csv"
If dlg.ShowDialog() = DialogResult.OK Then
Using reader As New StreamReader(dlg.FileName, Encoding.UTF8)
Dim lineNumber As Integer = 0
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
lineNumber += 1
' Process each line...
End While
End Using
End If
End Using
The StreamReader approach is more memory-efficient for large files because it reads line by line instead of loading the entire file into memory. For more on reading text files, see the VB.NET Read Text File guide.
All important properties at a glance
| Property | Default | Purpose |
|---|---|---|
Filter | (empty) | File type dropdown entries |
FilterIndex | 1 | Pre-selected filter (1-based) |
FileName | (empty) | Selected file path after dialog closes |
FileNames | (empty) | All selected paths when Multiselect is True |
InitialDirectory | (empty) | Starting folder |
Multiselect | False | Allow selecting multiple files |
CheckFileExists | True | Reject paths that don’t exist |
CheckPathExists | True | Reject invalid folder paths |
Title | „Open“ | Dialog window title |
ReadOnlyChecked | False | Pre-check the „Open as read-only“ checkbox |
Common mistakes
Reading without checking DialogResult
' BAD - FileName is empty if user cancels
dlg.ShowDialog()
Dim content = File.ReadAllText(dlg.FileName)
' GOOD - check the result first
If dlg.ShowDialog() = DialogResult.OK Then
Dim content = File.ReadAllText(dlg.FileName)
End If
Using FileName instead of FileNames with Multiselect
' BAD - only gets the first file when Multiselect is True
dlg.Multiselect = True
If dlg.ShowDialog() = DialogResult.OK Then
Dim content = File.ReadAllText(dlg.FileName) ' only first file!
End If
' GOOD - iterate all selected files
If dlg.ShowDialog() = DialogResult.OK Then
For Each path In dlg.FileNames
Dim content = File.ReadAllText(path)
Next
End If
Forgetting the Using block
The OpenFileDialog holds unmanaged resources (a Windows COM object). Without Using, those resources stay in memory until the garbage collector runs. Always wrap it in a Using block.
FAQ
Create a New OpenFileDialog() inside a Using block, set the Filter property, then call ShowDialog(). Check if the result equals DialogResult.OK before reading from dlg.FileName.
Set dlg.Multiselect = True before calling ShowDialog(). The user can then hold Ctrl or Shift to select several files. Access all paths through dlg.FileNames (plural).
Set the Filter property with the pattern Description|*.ext. For multiple types, separate with pipes: "Text (*.txt)|*.txt|CSV (*.csv)|*.csv". Combine extensions with semicolons: "Images (*.png;*.jpg)|*.png;*.jpg".
No. The dialog only lets the user pick a file path. You are responsible for reading the file yourself using methods like File.ReadAllText(), File.ReadAllBytes(), or a StreamReader with the path from dlg.FileName.
OpenFileDialog lets the user pick an existing file to read, with file-exists validation built in. SaveFileDialog lets the user pick a destination for writing a new file, with overwrite confirmation. Both return the selected path through FileName.
Wrapping up
The OpenFileDialog gives your VB.NET application a native file-open experience with minimal code. Set a Filter for file types, enable Multiselect for batch operations, and always check DialogResult.OK before reading. For the actual file reading, use File.ReadAllText() for text or File.ReadAllBytes() for binary data. Need to save files instead? The SaveFileDialog follows the same pattern. For more on reading text files, see the VB.NET Read Text File guide.