This documentation is archived and is not being maintained.

OpenFileDialog Class

Prompts the user to open a file. This class cannot be inherited.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public NotInheritable Class OpenFileDialog _
	Inherits FileDialog
'Usage
Dim instance As OpenFileDialog

This class allows you to check whether a file exists and to open it. The ShowReadOnly property determines whether a read-only check box appears in the dialog box. The ReadOnlyChecked property indicates whether the read-only check box is checked.

Most of the functionality for this class is found in the FileDialog class.

If you want to give the user the ability to select a folder instead of a file, use FolderBrowserDialog instead.

The following code example creates an OpenFileDialog, sets several properties, and displays the dialog box using the CommonDialog.ShowDialog method. The example requires a form with a Button placed on it and the System.IO namespace added to it.

Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myStream As Stream = Nothing 
    Dim openFileDialog1 As New OpenFileDialog()

    openFileDialog1.InitialDirectory = "c:\"
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    openFileDialog1.FilterIndex = 2
    openFileDialog1.RestoreDirectory = True 

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 
        Try
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then 
                ' Insert code to read the stream here. 
            End If 
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally 
            ' Check this again, since we need to make sure we didn't throw an exception on open. 
            If (myStream IsNot Nothing) Then
                myStream.Close()
            End If 
        End Try 
    End If 
End Sub

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0
Show: