Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-open-files-using-the-openfiledialog-component.md

3.6 KiB

title, ms.date, description, dev_langs, helpviewer_keywords, ms.assetid
title ms.date description dev_langs helpviewer_keywords ms.assetid
How to: Open files with the OpenFileDialog component 02/11/2019 Learn how to use the OpenFileDialog component to open the Windows dialog box for browsing and selecting files.
csharp
vb
OpenFileDialog component [Windows Forms], opening files
OpenFile method [Windows Forms], OpenFileDialog component
files [Windows Forms], opening with OpenFileDialog component
9d88367a-cc21-4ffd-be74-89fd63767d35

How to: Open files with the OpenFileDialog

The xref:System.Windows.Forms.OpenFileDialog?displayProperty=nameWithType component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the xref:System.Windows.Forms.OpenFileDialog.OpenFile%2A?displayProperty=nameWithType method, or create an instance of the xref:System.IO.StreamReader?displayProperty=nameWithType class. The following examples show both approaches.

In .NET Framework, to get or set the xref:System.Windows.Forms.FileDialog.FileName%2A property requires a privilege level granted by the xref:System.Security.Permissions.FileIOPermission?displayProperty=nameWithType class. The examples run a xref:System.Security.Permissions.FileIOPermission 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.

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 or Build 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 <folder name>.csproj project file.

Example: Read a file as a stream with StreamReader

The following example uses the Windows Forms xref:System.Windows.Forms.Button control's xref:System.Windows.Forms.Control.Click event handler to open the xref:System.Windows.Forms.OpenFileDialog with the xref:System.Windows.Forms.CommonDialog.ShowDialog%2A method. After the user chooses a file and selects OK, an instance of the xref:System.IO.StreamReader class reads the file and displays its contents in the form's text box. For more information about reading from file streams, see xref:System.IO.FileStream.BeginRead%2A?displayProperty=nameWithType and xref:System.IO.FileStream.Read%2A?displayProperty=nameWithType.

[!code-csharpOpenFileDialog#1] [!code-vbOpenFileDialog#1]

Example: Open a file from a filtered selection with OpenFile

The following example uses the xref:System.Windows.Forms.Button control's xref:System.Windows.Forms.Control.Click event handler to open the xref:System.Windows.Forms.OpenFileDialog with a filter that shows only text files. After the user chooses a text file and selects OK, the xref:System.Windows.Forms.OpenFileDialog.OpenFile%2A method is used to open the file in Notepad.

[!code-csharpOpenFileDialog#2] [!code-vbOpenFileDialog#2]

See also