TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# FolderBrowserDialog Control

Use the FolderBrowserDialog control in Windows Forms to allow selection of a folder.
FolderBrowserDialog displays a directory selection window. Once the user selects a folder, we access it from the C# source. This control from Windows Forms provides a convenient way to select folders (not files).
Example. To add a FolderBrowserDialog to your Windows Forms project, please open the Toolbox by clicking on the View menu and then Toolbox. Next, double-click the FolderBrowserDialog entry.

And: 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.

Output: 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.

Code for FolderBrowserDialog: C# 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"); } } } }
The Form1_Load event is raised when the program starts up. It opens a FolderBrowserDialog. It calls ShowDialog to do this. When the ShowDialog method is called, execution stops in this method. It transfers to the new dialog.

And: 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.

Inside the if-statement, we know that the user clicked on the OK button on the FolderBrowserDialog. The C# code that is inside the if-statement reads in all the file paths in the folder selected into a string array.

Then: It displays the number of files found by counting the paths it read. A MessageBox reports the number of files found.

Properties. Let's look at the properties on the FolderBrowserDialog control. It is usually easiest to directly change the properties in the Properties pane. We access this by clicking on View and then Properties Window in Visual Studio.

Description: A text description that appears at the top of the dialog. The dialog in this article shows "The Dev Codes made this."

RootFolder: The RootFolder is where the dialog first begins. This is the initial folder.

SelectedPath: The folder that was selected when the dialog was exited. You can test the DialogResult first and then access this property.

ShowNewFolderButton: Whether the new folder button should be visible. Usually, it is acceptable to leave this enabled.

OpenFileDialog. Next, we consider the difference between the OpenFileDialog and the FolderBrowserDialog. The OpenFileDialog is meant for selecting files, and if you want to prompt your user to select a data file, use the OpenFileDialog.OpenFileDialog
However, the OpenFileDialog does not allow users to easily select folders themselves. The FolderBrowserDialog is ideal for actually selecting folders themselves. This site describes the OpenFileDialog in depth.
MessageBox. The MessageBox class provides the MessageBox.Show static method. The MessageBox.Show method contains many overloads and can be used to display simple alerts and informative dialogs.MessageBox

Note: The MessageBox.Show method currently does not show truly modern dialogs in appearance.

Summary. We looked at the FolderBrowserDialog control. We used it to select a directory from the file system in the UI. We opened FolderBrowserDialog, accessed the user's selection from the dialog when it is closed, and changed properties.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf