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.

Need .NET help?

Building a desktop application?

I've been developing professionally in VB.NET and C# for over 17 years. From file dialogs to complete WinForms applications, I can help.

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.MyDocuments for user documents
  • Environment.SpecialFolder.Desktop for 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.

Planning a project?

Need a polished WinForms application?

From file dialogs to complete data-driven desktop apps: I design software that lasts. Let's talk about your project.

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

PropertyDefaultPurpose
Filter(empty)File type dropdown entries
FilterIndex1Pre-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
MultiselectFalseAllow selecting multiple files
CheckFileExistsTrueReject paths that don’t exist
CheckPathExistsTrueReject invalid folder paths
Title„Open“Dialog window title
ReadOnlyCheckedFalsePre-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.

Need expert support?

Looking for an experienced .NET developer?

I'll take on your project, from file operations to finished desktop applications. Just drop me a message.

FAQ

How do I open an OpenFileDialog in VB.NET?

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.

How do I select multiple files with OpenFileDialog?

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).

How do I filter file types in OpenFileDialog?

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".

Does OpenFileDialog actually read the file?

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.

What is the difference between OpenFileDialog and SaveFileDialog?

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.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Robert Skibbe
Datenschutz-Übersicht

Diese Website verwendet Cookies, damit wir dir die bestmögliche Benutzererfahrung bieten können. Cookie-Informationen werden in deinem Browser gespeichert und führen Funktionen aus, wie das Wiedererkennen von dir, wenn du auf unsere Website zurückkehrst, und hilft unserem Team zu verstehen, welche Abschnitte der Website für dich am interessantesten und nützlichsten sind.