What is Folder Browser Dialog?
Folder Browser Dialog is a .Net control that prompts the user to browse and select a folder location. FolderBrowserDialog moreover looks like a Windows Explorer that gives us a feature to navigate to different folder locations.
FolderBrowserDialog is a class inherited from System.Windows.Forms namespace and cannot be further inherited.
The one is Design time and the other way is Run time. We are going to focus on runtime implementation.
(A) Design time – After the creation of Form, you can always drag and drop a FolderBrowserDialog control from toolbox to windows form. After adding this C# control, the will show on the bottom of the windows form. Please refer below screenshot.
C# FolderBrowserDialog in Design Mode.
1) Create a new Windows Forms Application. Name this project as “abcFolderBrowserDialog”, select the folder location, and click on the OK button.
2) For the ease of demonstration, I renamed “Program.cs” to “FBDialog.cs” and “Form1.cs” to “MainForm.cs” in solution explorer. In addition, I have also set the “Text” property of Windows Form to FolderBrowserDialog Example”
3) Now put controls on windows form as given in below screenshot –
In this example, I have taken the following controls –
i) Browse Button: Click on this button to open a FileBrowserDialog.
ii) TextBox1: A folder location selected using the Browse button will display in this textbox. Set the Enabled property to false.
iii) Show Files Button: Once a folder location is selected, then clicking on this button will show all files in ListBox1.
iv) ListBox1: Shows the list of all files available in a folder location browsed through the Browse button.
4) Double click on the Browse button to open up a MainForm.cs and write the following code.
private void button1_Click(object sender, EventArgs e) { // Create a new instance of FolderBrowserDialog. FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog(); // A new folder button will display in FolderBrowserDialog. folderBrowserDlg.ShowNewFolderButton = true; //Show FolderBrowserDialog DialogResult dlgResult = folderBrowserDlg.ShowDialog(); if (dlgResult.Equals(DialogResult.OK)) { //Show selected folder path in textbox1. textBox1.Text = folderBrowserDlg.SelectedPath; //Browsing start from root folder. Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder; } }
5) Again go back to MainForm in design mode and double click on the “Show Files” button to open MainForm.cs file again. In the 4th step, we have created an instance of FileBrowserDialog. Now in this step, we are going to add all files and folders in Listbox. Write the following code in MainForm.cs file –
private void button2_Click(object sender, EventArgs e) { if (!textBox1.Text.Equals(String.Empty)) { if (System.IO.Directory.GetFiles(textBox1.Text).Length > 0) { foreach (string file in System.IO.Directory.GetFiles(textBox1.Text)) { //Add file in ListBox. listBox1.Items.Add(file); } } else { listBox1.Items.Add(String.Format(“No files Found at location : {0}”, textBox1.Text)); } } }
6) Now code is ready to test the functionality of Folder Browse Dialog. Execute the application and see the output as given below –
Do you know:-
- How to Retrieve data using dataset in c#
- “Login failed for user” SQLConnection using C#
- How can I prevent the browser back button After Logout
- Calendar Control in c# Windows Application
- Timer tick event in c# windows application
- How to delete record in c# using the connected approach
- [Connect] to Database c# Through Code
- The Command Class | C# | TrickCode
- C# DataReader ADO.NET |Trickcode
- What is disconnected data access?
- how to Adding Parameters to Commands
- SQL Basics?
- What is Database Preparations?
- SQL vs NoSQL or MySQL vs MongoDB
- What is data provider in C#?
- What is connection string C#?
- [SQL] SELECT Statement | Example
- how to get return value from stored procedure in sql server
- how to prevent sql injection attacks
- What is Database? What is SQL?
- C#.Net How To: Send email in asp.net using c#
If you like the tutorial, then please share this tutorial with your friends on social media.
Post A Comment:
0 comments: