This documentation is archived and is not being maintained.

FolderBrowserDialog Class

Prompts the user to select a folder. This class cannot be inherited.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

Public NotInheritable Class FolderBrowserDialog _
	Inherits CommonDialog

The FolderBrowserDialog type exposes the following members.

Public methodFolderBrowserDialogInitializes a new instance of the FolderBrowserDialog class.

Protected propertyCanRaiseEventsGets a value indicating whether the component can raise an event. (Inherited from Component.)
Public propertyContainerGets the IContainer that contains the Component. (Inherited from Component.)
Public propertyDescriptionGets or sets the descriptive text displayed above the tree view control in the dialog box.
Protected propertyDesignModeGets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Protected propertyEventsGets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public propertyRootFolderGets or sets the root folder where the browsing starts from.
Public propertySelectedPathGets or sets the path selected by the user.
Public propertyShowNewFolderButtonGets or sets a value indicating whether the New Folder button appears in the folder browser dialog box.
Public propertySiteGets or sets the ISite of the Component. (Inherited from Component.)
Public propertyTagGets or sets an object that contains data about the control. (Inherited from CommonDialog.)

Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodDisposeReleases all resources used by the Component. (Inherited from Component.)
Protected methodDispose(Boolean)Releases the unmanaged resources used by the Component and optionally releases the managed resources. (Inherited from Component.)
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeReleases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodGetServiceReturns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodHookProcDefines the common dialog box hook procedure that is overridden to add specific functionality to a common dialog box. (Inherited from CommonDialog.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Protected methodOnHelpRequestRaises the HelpRequest event. (Inherited from CommonDialog.)
Protected methodOwnerWndProcDefines the owner window procedure that is overridden to add specific functionality to a common dialog box. (Inherited from CommonDialog.)
Public methodResetResets properties to their default values. (Overrides CommonDialog.Reset.)
Protected methodRunDialogWhen overridden in a derived class, specifies a common dialog box. (Inherited from CommonDialog.)
Public methodShowDialogRuns a common dialog box with a default owner. (Inherited from CommonDialog.)
Public methodShowDialog(IWin32Window)Runs a common dialog box with the specified owner. (Inherited from CommonDialog.)
Public methodToStringReturns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)

Public eventDisposedOccurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Public eventHelpRequestInfrastructure. 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.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO

Public Class FolderBrowserDialogExampleForm 
    Inherits Form

    Private folderBrowserDialog1 As FolderBrowserDialog
    Private openFileDialog1 As OpenFileDialog 

    Private richTextBox1 As RichTextBox

    Private mainMenu1 As MainMenu
    Private fileMenuItem As MenuItem
    Private WithEvents folderMenuItem As MenuItem, _
                       closeMenuItem As MenuItem, _
                       openMenuItem As MenuItem

    Private openFileName As String, folderName As String

    Private fileOpened As Boolean = False

    Public Sub New()
        Me.mainMenu1 = New System.Windows.Forms.MainMenu() 
        Me.fileMenuItem = New System.Windows.Forms.MenuItem() 
        Me.openMenuItem = New System.Windows.Forms.MenuItem() 
        Me.folderMenuItem = New System.Windows.Forms.MenuItem() 
        Me.closeMenuItem = New System.Windows.Forms.MenuItem() 

        Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog() 
        Me.folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog() 
        Me.richTextBox1 = New System.Windows.Forms.RichTextBox() 

        Me.fileMenuItem.MenuItems.AddRange( _
                    New System.Windows.Forms.MenuItem() {Me.openMenuItem, _
                                                         Me.closeMenuItem, _
        Me.fileMenuItem.Text = "File" 

        Me.openMenuItem.Text = "Open..." 

        Me.folderMenuItem.Text = "Select Directory..." 

        Me.closeMenuItem.Text = "Close" 
        Me.closeMenuItem.Enabled = False 

        Me.openFileDialog1.DefaultExt = "rtf" 
        Me.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf" 

        ' Set the Help text description for the FolderBrowserDialog.
        Me.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.
        Me.folderBrowserDialog1.ShowNewFolderButton = False 

        ' Default to the My Documents folder.
        Me.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal 

        Me.richTextBox1.AcceptsTab = True 
        Me.richTextBox1.Location = New System.Drawing.Point(8, 8) 
        Me.richTextBox1.Size = New System.Drawing.Size(280, 344) 
        Me.richTextBox1.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or _
                                 AnchorStyles.Bottom Or AnchorStyles.Right 

        Me.ClientSize = New System.Drawing.Size(296, 360) 
        Me.Menu = Me.mainMenu1 
        Me.Text = "RTF Document Browser" 
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New FolderBrowserDialogExampleForm())
    End Sub

    ' Bring up a dialog to open a file.
    Private Sub openMenuItem_Click(sender As object, e As System.EventArgs) _
        Handles openMenuItem.Click
        ' If a file is not opened, then set the initial directory to the
        ' FolderBrowserDialog.SelectedPath value.
        If (not fileOpened) Then
            openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath 
            openFileDialog1.FileName = nothing 
        End If

        ' Display the openFile dialog.
        Dim result As DialogResult = openFileDialog1.ShowDialog() 

        ' OK button was pressed.
        If (result = DialogResult.OK) Then
            openFileName = openFileDialog1.FileName 
                ' Output the requested file in richTextBox1.
                Dim s As Stream = openFileDialog1.OpenFile() 
                richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText) 

                fileOpened = True 

            Catch exp As Exception
                MessageBox.Show("An error occurred while attempting to load the file. The error is:" _
                                + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine) 
                fileOpened = False 
            End Try

            closeMenuItem.Enabled = fileOpened 

        ' Cancel button was pressed.
        ElseIf (result = DialogResult.Cancel) Then
        End If
    End Sub

    ' Close the current file.
    Private Sub closeMenuItem_Click(sender As object, e As System.EventArgs) _
        Handles closeMenuItem.Click
        richTextBox1.Text = "" 
        fileOpened = False 

        closeMenuItem.Enabled = False 
    End Sub

    ' Bring up a dialog to chose a folder path in which to open or save a file.
    Private Sub folderMenuItem_Click(sender As object, e As System.EventArgs) _
        Handles folderMenuItem.Click
        ' Show the FolderBrowserDialog.
        Dim result As DialogResult = folderBrowserDialog1.ShowDialog() 

        If ( result = DialogResult.OK ) Then
            folderName = folderBrowserDialog1.SelectedPath 
            If (not fileOpened) Then
                ' No file is opened, bring up openFileDialog in selected path.
                openFileDialog1.InitialDirectory = folderName 
                openFileDialog1.FileName = nothing
            End If
        End If
    End Sub

End Class

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.