--- title: "How to: Open files with the OpenFileDialog component" ms.date: "02/11/2019" description: Learn how to use the OpenFileDialog component to open the Windows dialog box for browsing and selecting files. dev_langs: - "csharp" - "vb" helpviewer_keywords: - "OpenFileDialog component [Windows Forms], opening files" - "OpenFile method [Windows Forms], OpenFileDialog component" - "files [Windows Forms], opening with OpenFileDialog component" ms.assetid: 9d88367a-cc21-4ffd-be74-89fd63767d35 --- # How to: Open files with the OpenFileDialog The component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the method, or create an instance of the class. The following examples show both approaches. In .NET Framework, to get or set the property requires a privilege level granted by the class. The examples run a permission check, and can throw an exception due to insufficient privileges if run in a partial-trust context. For more information, see [Code access security basics](/dotnet/framework/misc/code-access-security-basics). You can build and run these examples as .NET Framework apps from the C# or Visual Basic command line. For more information, see [Command-line building with csc.exe](/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe) or [Build from the command line](/dotnet/visual-basic/reference/command-line-compiler/building-from-the-command-line). Starting with .NET Core 3.0, you can also build and run the examples as Windows .NET Core apps from a folder that has a .NET Core Windows Forms *\.csproj* project file. ## Example: Read a file as a stream with StreamReader The following example uses the Windows Forms control's event handler to open the with the method. After the user chooses a file and selects **OK**, an instance of the class reads the file and displays its contents in the form's text box. For more information about reading from file streams, see and . [!code-csharp[OpenFileDialog#1](~/samples/snippets/winforms/open-files/example1/cs/Form1.cs)] [!code-vb[OpenFileDialog#1](~/samples/snippets/winforms/open-files/example1/vb/Form1.vb)] ## Example: Open a file from a filtered selection with OpenFile The following example uses the control's event handler to open the with a filter that shows only text files. After the user chooses a text file and selects **OK**, the method is used to open the file in Notepad. [!code-csharp[OpenFileDialog#2](~/samples/snippets/winforms/open-files/example2/cs/Form1.cs)] [!code-vb[OpenFileDialog#2](~/samples/snippets/winforms/open-files/example2/vb/Form1.vb)] ## See also - - [OpenFileDialog component](openfiledialog-component-windows-forms.md)