OpenFileDialog Component in VB.NET

The Windows Forms OpenFileDialog component is a pre-configured dialog box.
  • 3885

The Windows Forms OpenFileDialog component is a pre-configured dialog box. It is the same Open File dialog box exposed by the Windows operating system. It inherits from the CommonDialog class.

Use this component within your Windows application as a simple solution for file selection in lieu of configuring your own dialog box. By relying on standard Windows dialog boxes, you create applications whose basic functionality is immediately familiar to users. Be aware, however, that when using the OpenFileDialog component, you must write your own file-opening logic.

Use the ShowDialog method to display the dialog at run time. You can enable users to multi-select files to be opened with the Multiselect property. Additionally, you can use the ShowReadOnly property to determine if a read-only check box appears in the dialog box. The ReadOnlyChecked property indicates whether the read-only check box is selected. Finally, the Filter property sets the current file name filter string, which determines the choices that appear in the "Files of type" box in the dialog box.

When it is added to a form, the OpenFileDialog component appears in the tray at the bottom of the Windows Forms Designer

Opening Files Using the OpenFileDialog Component

The OpenFileDialog component allows users to browse the folders of their computer or any computer on the network and select one or more files to open. The dialog box returns the path and name of the file the user selected in the dialog box.

Once the user has selected the file to be opened, there are two approaches to the mechanism of opening the file. If you prefer to work with file streams, you can create an instance of the StreamReader class. Alternately, you can use the OpenFile method to open the selected file.

The first example below involves a FileIOPermission permission check (as described in the Security Note), but gives you access to the filename. You can use this technique from the Local Machine, Intranet, and Internet zones. The second method also does a FileIOPermission permission check, but is better suited for applications in the Intranet or Internet zones.

To open a file as a stream using the OpenFileDialog component

  • Display the Open File dialog box and call a method to open the file selected by the user.
    One approach is to use the ShowDialog method to display the Open File dialog box, and use an instance of the StreamReader class to open the file.

    The example below uses the Button control's Click event handler to open an instance of the OpenFileDialog component. When a file is chosen and the user clicks OK, the file selected in the dialog box opens. In this case, the contents are displayed in a message box, just to show that the file stream has been read.
    Security Note   To get or set the FileName property, your assembly requires a privilege level granted by the System.Security.Permissions.FileIOPermission class. If you are running in a partial-trust context, the process might throw an exception due to insufficient privileges. For more information, see Code Access Security Basics.
    The example assumes your form has a Button control and an OpenFileDialog component.

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

                If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                Dim sr As New System.IO.StreamReader(OpenFileDialog1.FileName)
            End If
        End Sub


    When we will click on the Button1 the screen will come :


    Here I selected table format notepad file.

    The result will be :


    Content of the table format file came in message box.

Source Code

Source code attach in the top of the article.


More Articles

© 2020 DotNetHeaven. All rights reserved.