* Merge winforms framework content to working branch (#14) * Breadcrumb / TOC / Move net5 to net folder (#15) * change path from net5 to net * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Mess with bread/toc * Add .net 5 winforms placeholder article (#16) * added some metadata and adjusted net5 placeholder * corrections * corrections * corrections * Test1 * Swapping landing page vs concept * fix links * Fix links * Fix desc
3.7 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. |
|
|
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]