OpenFileDialog Control

The component of OpenFileDialog allows users to browse their computer's folders or any computer on the network and select one or more open files.

The dialog box returns the path and name of the file selected by the user in the dialog box. There are two approaches to the file opening mechanism once the user has selected the file to be opened.

If we prefer to work with file streams, we can create an instance of the StreamReader class. Alternately, we can use the OpenFile method to open the selected file.

Open File Dialog Box - UK Academe

The property of FileName can be set before the dialog box is displayed. This causes the initial display of the given filename in the dialog box. In most cases, our applications should set the InitialDirectory property, Filter, and FilterIndex properties prior to calling ShowDialog.

For Example:

The example below uses the Click event handler of the button control to open an instance of the component of OpenFileDialog. The file selected in the dialog box opens when a file is selected and the user clicks OK. In this case, the contents are displayed in a message box to show that the file stream has been read.

The example assumes that our form has a control button and a component for OpenFileDialog.


private void btnBrowse_Click(object sender, EventArgs e)
            var fileContent = string.Empty;
                filePath = openFileDialog1.FileName;
                txtFilePath.Text = filePath;
                var fileStram = openFileDialog1.OpenFile();
                using (StreamReader reader = new StreamReader(fileStram))
                    fileContent = reader.ReadToEnd();
            MessageBox.Show(fileContent, "File Content at Path:" + filePath, MessageBoxButtons.OK);


Open File Dialog Box Control

Video Tutorial