Home
Map
FolderBrowserDialog ControlUse the FolderBrowserDialog control in Windows Forms to allow selection of a folder.
WinForms
This page was last reviewed on Oct 5, 2022.
FolderBrowserDialog. This displays a directory selection window. Once the user selects a folder, we access it from the C# source. This is a convenient way to select folders (not files).
OpenFileDialog
Getting started. To add a FolderBrowserDialog to your Windows Forms project, please open the Toolbox by clicking on the View menu and then Toolbox.
Example code. First, double-click the FolderBrowserDialog entry. In the bottom part of your window, a FolderBrowserDialog box will be displayed.
Next We create a Load event on the Form to display the dialog by double-clicking on the window.
Detail On startup, it shows the dialog. You can select a folder and press OK. It will then display the number of files in the folder.
Info The Form1_Load event is raised when the program starts up. It opens a FolderBrowserDialog. It calls ShowDialog to do this.
using System; using System.IO; using System.Windows.Forms; namespace WindowsFormsApplication1 // Will be application-specific { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // // This event handler was created by double-clicking the window in the designer. // It runs on the program's startup routine. // DialogResult result = folderBrowserDialog1.ShowDialog(); if (result == DialogResult.OK) { // // The user selected a folder and pressed the OK button. // We print the number of files found. // string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath); MessageBox.Show("Files found: " + files.Length.ToString(), "Message"); } } } }
ShowDialog. When the ShowDialog method is called, execution stops in this method. When the user clicks on OK or Cancel in the dialog, control flow returns here.
Then We test the DialogResult enumerated type for the special value DialogResult.OK.
Properties. Let us look at the properties on the FolderBrowserDialog control. It is usually easiest to directly change the properties in the Properties pane.
Detail A text description that appears at the top of the dialog. The dialog in this article shows "Dot Net Perls made this."
Detail The RootFolder is where the dialog first begins. This is the initial folder.
Detail The folder that was selected when the dialog was exited. You can test the DialogResult first and then access this property.
Detail Whether the new folder button should be visible. Usually, it is acceptable to leave this enabled.
MessageBox. The MessageBox.Show method contains many overloads and can be used to display simple alerts and informative dialogs. It is simple to use.
MessageBox.Show
Summary. We used FolderBrowserDialog to select a directory from the file system in the UI. We accessed the user's selection from the dialog when it is closed, and changed properties.
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 Oct 5, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.