In this article I will explain with an example, how to use the Windows Forms (WinForms) OpenFileDialog Box control in C# and VB.Net.
In Windows Forms (WinForms) applications, the OpenFileDialog Box is used to select single or multiple files from the Windows Folders or Directories.
 
 
Form Design
The following Windows Form consists of two Buttons.
Windows Forms (WinForms) OpenFileDialog Box Tutorial with example in C# and VB.Net
 
Then you need to drag and add an OpenFileDialog control to the form from the Dialogs section of the Visual Studio ToolBox.
Windows Forms (WinForms) OpenFileDialog Box Tutorial with example in C# and VB.Net
 
Once added it will be displayed in the section below the Form design as shown below.
Windows Forms (WinForms) OpenFileDialog Box Tutorial with example in C# and VB.Net
 
 
Namespaces
You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 
Selecting Single File with the OpenFileDialog Box
Inside this event handler, the Multiselect property of the OpenFileDialog Box needs to be set to False which is its default value in order to select a Single File.
The Path of the selected File is available in the FileName property of the OpenFileDialog Box.
Finally, the Name and Path of the selected File is displayed in the Windows Forms MessageBox.
C#
private void btnSelect_Click(object sender, EventArgs e)
{
    openFileDialog1.Multiselect = false;
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string fileName = Path.GetFileName(openFileDialog1.FileName);
        string filePath = openFileDialog1.FileName;
        MessageBox.Show(fileName + " - " + filePath);
    }
}
 
VB.Net
Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnSelect.Click
    openFileDialog1.Multiselect = False
    If openFileDialog1.ShowDialog() = DialogResult.OK Then
        Dim fileName As String = Path.GetFileName(openFileDialog1.FileName)
        Dim filePath As String = openFileDialog1.FileName
        MessageBox.Show(fileName & " - " & filePath)
    End If
End Sub
 
Screenshot
Windows Forms (WinForms) OpenFileDialog Box Tutorial with example in C# and VB.Net
 
 
Selecting Multiple Files with the OpenFileDialog Box
Inside this event handler, the Multiselect property of the OpenFileDialog Box needs to be set to True in order to select a Multiple Files.
The Path of the selected File is available in the FileName property of the OpenFileDialog Box.
Finally, the Name and Path of the selected Files are displayed in the Windows Forms MessageBox.
C#
private void btnSelectMultiple_Click(object sender, EventArgs e)
{
    string message = "";
    openFileDialog1.Multiselect = true;
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        foreach (string file in openFileDialog1.FileNames)
        {
            message += Path.GetFileName(file) + " - " + file + Environment.NewLine;
        }
        MessageBox.Show(message);
    }
}
 
VB.Net
Private Sub btnSelectMultiple_Click(sender As System.Object, e As System.EventArgs) Handles btnSelectMultiple.Click
    Dim message As String = ""
    openFileDialog1.Multiselect = True
    If openFileDialog1.ShowDialog() = DialogResult.OK Then
        For Each file As String In openFileDialog1.FileNames
            message += Path.GetFileName(file) & " - " & file & Environment.NewLine
        Next
        MessageBox.Show(message)
    End If
End Sub
 
Screenshot
Windows Forms (WinForms) OpenFileDialog Box Tutorial with example in C# and VB.Net
 
 
Downloads