FolderBrowserDialog Class
Prompts the user to select a folder. This class cannot be inherited.
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.CommonDialog
System.Windows.Forms.FolderBrowserDialog
Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
The FolderBrowserDialog type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Container | Gets the IContainer that contains the Component. (Inherited from Component.) |
![]() | Description | Gets or sets the descriptive text displayed above the tree view control in the dialog box. |
![]() | RootFolder | Gets or sets the root folder where the browsing starts from. |
![]() | SelectedPath | Gets or sets the path selected by the user. |
![]() | ShowNewFolderButton | Gets or sets a value indicating whether the New Folder button appears in the folder browser dialog box. |
![]() | Site | Gets or sets the ISite of the Component. (Inherited from Component.) |
![]() | Tag | Gets or sets an object that contains data about the control. (Inherited from CommonDialog.) |
| Name | Description | |
|---|---|---|
![]() | CreateObjRef | Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.) |
![]() | Dispose() | Releases all resources used by the Component. (Inherited from Component.) |
![]() | Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetLifetimeService | Retrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | InitializeLifetimeService | Obtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.) |
![]() | Reset | Resets properties to their default values. (Overrides CommonDialog.Reset().) |
![]() | ShowDialog() | Runs a common dialog box with a default owner. (Inherited from CommonDialog.) |
![]() | ShowDialog(IWin32Window) | Runs a common dialog box with the specified owner. (Inherited from CommonDialog.) |
![]() | ToString | Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.) |
| Name | Description | |
|---|---|---|
![]() | Disposed | Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.) |
![]() | HelpRequest | Infrastructure. Occurs when the user clicks the Help button on the dialog box. |
This class provides a way to prompt the user to browse, create, and eventually select a folder. Use this class when you only want to allow the user to select folders, not files. Browsing of the folders is done through a tree control. Only folders from the file system can be selected; virtual folders cannot.
Typically, after creating a new FolderBrowserDialog, you set the RootFolder to the location from which to start browsing. Optionally, you can set the SelectedPath to an absolute path of a subfolder of RootFolder that will initially be selected. You can also optionally set the Description property to provide additional instructions to the user. Finally, call the ShowDialog method to display the dialog box to the user. When the dialog box is closed and the dialog box result from ShowDialog is DialogResult.OK, the SelectedPath will be a string containing the path to the selected folder.
You can use the ShowNewFolderButton property to control if the user is able to create new folders with the New Folder button.
FolderBrowserDialog is a modal dialog box; therefore, when shown, it blocks the rest of the application until the user has chosen a folder. When a dialog box is displayed modally, no input (keyboard or mouse click) can occur except to objects on the dialog box. The program must hide or close the dialog box (usually in response to some user action) before input to the calling program can occur.
The following code example creates an application that enables the user to open rich text (.rtf) files within the RichTextBox control.
// The following example displays an application that provides the ability to // open rich text files (rtf) into the RichTextBox. The example demonstrates // using the FolderBrowserDialog to set the default directory for opening files. // The OpenFileDialog class is used to open the file. using System; using System.Drawing; using System.Windows.Forms; using System.IO; public class FolderBrowserDialogExampleForm : System.Windows.Forms.Form { private FolderBrowserDialog folderBrowserDialog1; private OpenFileDialog openFileDialog1; private RichTextBox richTextBox1; private MainMenu mainMenu1; private MenuItem fileMenuItem, openMenuItem; private MenuItem folderMenuItem, closeMenuItem; private string openFileName, folderName; private bool fileOpened = false; // The main entry point for the application. [STAThreadAttribute] static void Main() { Application.Run(new FolderBrowserDialogExampleForm()); } // Constructor. public FolderBrowserDialogExampleForm() { this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.fileMenuItem = new System.Windows.Forms.MenuItem(); this.openMenuItem = new System.Windows.Forms.MenuItem(); this.folderMenuItem = new System.Windows.Forms.MenuItem(); this.closeMenuItem = new System.Windows.Forms.MenuItem(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog(); this.richTextBox1 = new System.Windows.Forms.RichTextBox(); this.mainMenu1.MenuItems.Add(this.fileMenuItem); this.fileMenuItem.MenuItems.AddRange( new System.Windows.Forms.MenuItem[] {this.openMenuItem, this.closeMenuItem, this.folderMenuItem}); this.fileMenuItem.Text = "File"; this.openMenuItem.Text = "Open..."; this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click); this.folderMenuItem.Text = "Select Directory..."; this.folderMenuItem.Click += new System.EventHandler(this.folderMenuItem_Click); this.closeMenuItem.Text = "Close"; this.closeMenuItem.Click += new System.EventHandler(this.closeMenuItem_Click); this.closeMenuItem.Enabled = false; this.openFileDialog1.DefaultExt = "rtf"; this.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf"; // Set the help text description for the FolderBrowserDialog. this.folderBrowserDialog1.Description = "Select the directory that you want to use as the default."; // Do not allow the user to create new files via the FolderBrowserDialog. this.folderBrowserDialog1.ShowNewFolderButton = false; // Default to the My Documents folder. this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal; this.richTextBox1.AcceptsTab = true; this.richTextBox1.Location = new System.Drawing.Point(8, 8); this.richTextBox1.Size = new System.Drawing.Size(280, 344); this.richTextBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right; this.ClientSize = new System.Drawing.Size(296, 360); this.Controls.Add(this.richTextBox1); this.Menu = this.mainMenu1; this.Text = "RTF Document Browser"; } // Bring up a dialog to open a file. private void openMenuItem_Click(object sender, System.EventArgs e) { // If a file is not opened, then set the initial directory to the // FolderBrowserDialog.SelectedPath value. if (!fileOpened) { openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath; openFileDialog1.FileName = null; } // Display the openFile dialog. DialogResult result = openFileDialog1.ShowDialog(); // OK button was pressed. if(result == DialogResult.OK) { openFileName = openFileDialog1.FileName; try { // Output the requested file in richTextBox1. Stream s = openFileDialog1.OpenFile(); richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText); s.Close(); fileOpened = true; } catch(Exception exp) { MessageBox.Show("An error occurred while attempting to load the file. The error is:" + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine); fileOpened = false; } Invalidate(); closeMenuItem.Enabled = fileOpened; } // Cancel button was pressed. else if(result == DialogResult.Cancel) { return; } } // Close the current file. private void closeMenuItem_Click(object sender, System.EventArgs e) { richTextBox1.Text = ""; fileOpened = false; closeMenuItem.Enabled = false; } // Bring up a dialog to chose a folder path in which to open or save a file. private void folderMenuItem_Click(object sender, System.EventArgs e) { // Show the FolderBrowserDialog. DialogResult result = folderBrowserDialog1.ShowDialog(); if( result == DialogResult.OK ) { folderName = folderBrowserDialog1.SelectedPath; if(!fileOpened) { // No file is opened, bring up openFileDialog in selected path. openFileDialog1.InitialDirectory = folderName; openFileDialog1.FileName = null; openMenuItem.PerformClick(); } } } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
