C# Windows Forms FolderBrowserDialog Examples

Let us look at a simple C# Windows Forms FolderBrowserDialog example.

Example 1: FolderBrowserDialog

This example will comprise the following files:

  • FolderBrowserDialog.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). FolderBrowserDialog.cs

Create a file named FolderBrowserDialog.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace FolderBrowserDialogExample {
  class Form1 : Form {
    // The main entry point for the application.
    [STAThread]
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      button.Text = "Folder...";
      button.Location = new System.Drawing.Point(10, 10);
      button.Click += delegate(object sender, EventArgs e) {
        FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
        folderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
        folderBrowserDialog.SelectedPath = "C:\Users\Yves\Projects\xtd";
        if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
          label.Text = string.Format("Path = {0}", folderBrowserDialog.SelectedPath);
      };

      label.Text = "Path = ";
      label.Location = new System.Drawing.Point(10, 40);
      label.Width = 340;

      Text = "FolderBrowserDialog example";
      Controls.AddRange(new Control[] { button, label});
    }

    private Button button = new Button();
    private Label label = new Label();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts