C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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");
}
}
}
}
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.
Then: It displays the number of files found by counting the paths it read. A MessageBox reports the number of files found.
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.
Note: The MessageBox.Show method currently does not show truly modern dialogs in appearance.