Home
Map
OpenFileDialog ExampleUse the OpenFileDialog control in Windows Forms to enable the selection of a file.
WinForms
This page was last reviewed on Feb 18, 2023.
OpenFileDialog. This allows users to browse folders and select files. It can be used with C# code. It displays the standard Windows dialog box.
Dialog result value. The results of the selection made in OpenFileDialog can be read in your C# code. We can use common methods like File.ReadAllText once the dialog is closed.
SaveFileDialog
ShowDialog. You can open the OpenFileDialog that is in your Windows Forms program. The dialog will not open automatically and it must be invoked in your custom code.
Detail You will want to add an event handler in the Windows Forms program to open the dialog in your C# code.
Detail You can add a Button control. Find the Button icon in the Toolbox and drag it to an area in your Windows Forms window.
Button
Tip You can add an event to the button click by double-clicking on the "button1" in the designer.
Here This DialogResult variable is tested to see what action the user specified to take.
DialogResult
using System; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Show the dialog and get result. DialogResult result = openFileDialog1.ShowDialog(); if (result == DialogResult.OK) // Test result. { } Console.WriteLine(result); // <-- For debugging use. } } }
Read files. You can access the file specified by the user in the OpenFileDialog—and then read it in using the System.IO namespace methods. We can also handle exceptions.
Note In this example, when you click on the button the dialog will ask you what file you want.
And When you accept the dialog, the code will read in that file and print its size in bytes.
using System; using System.IO; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { int size = -1; DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog. if (result == DialogResult.OK) // Test result. { string file = openFileDialog1.FileName; try { string text = File.ReadAllText(file); size = text.Length; } catch (IOException) { } } Console.WriteLine(size); // <-- Shows file size in debugging mode. Console.WriteLine(result); // <-- For debugging use. } } }
Properties. The OpenFileDialog control in Windows Forms has many properties that you can set directly in the designer. You do not need to assign them in your own C# code.
Filter. The OpenFileDialog supports filters for matching file names. The asterisk indicates a wildcard. With an extension, you can filter by a file type.
ReadOnly. The OpenFileDialog has some properties that allow users to specify whether a file should be read in read-only mode. The read-only check box can be displayed.
Summary. We looked at the OpenFileDialog—many programs will need to have an open file dialog. This control implements this functionality without any problems.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Feb 18, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.