Files
docs-desktop/dotnet-desktop-guide/framework/winforms/controls/how-to-choose-folders-with-the-windows-forms-folderbrowserdialog-component.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

4.0 KiB

title, description, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title description ms.date dev_langs helpviewer_keywords ms.assetid
Choose Folders with FolderBrowserDialog Component Learn how to use the Windows Forms FolderBrowserDialog component within Windows applications you create to prompt users to select a folder. 03/30/2017
csharp
vb
cpp
directories [Windows Forms], choosing
FolderBrowserDialog component [Windows Forms], choosing directories
folders [Windows Forms], selecting
folders [Windows Forms], choosing
directories [Windows Forms], selecting
4593670e-7c7d-4661-b46b-4ffb63258adb

How to: Choose Folders with the Windows Forms FolderBrowserDialog Component

Often, within Windows applications you create, you will have to prompt users to select a folder, most frequently to save a set of files. The Windows Forms xref:System.Windows.Forms.FolderBrowserDialog component allows you to easily accomplish this task.

To choose folders with the FolderBrowserDialog component

  1. In a procedure, check the xref:System.Windows.Forms.FolderBrowserDialog component's xref:System.Windows.Forms.Form.DialogResult%2A property to see how the dialog box was closed and get the value of the xref:System.Windows.Forms.FolderBrowserDialog component's xref:System.Windows.Forms.FolderBrowserDialog.SelectedPath%2A property.

  2. If you need to set the top-most folder that will appear within the tree view of the dialog box, set the xref:System.Windows.Forms.FolderBrowserDialog.RootFolder%2A property, which takes a member of the xref:System.Environment.SpecialFolder enumeration.

  3. Additionally, you can set the xref:System.Windows.Forms.FolderBrowserDialog.Description%2A property, which specifies the text string that appears at the top of the folder-browser tree view.

    In the example below, the xref:System.Windows.Forms.FolderBrowserDialog component is used to select a folder, similar to when you create a project in Visual Studio and are prompted to select a folder to save it in. In this example, the folder name is then displayed in a xref:System.Windows.Forms.TextBox control on the form. It is a good idea to place the location in an editable area, such as a xref:System.Windows.Forms.TextBox control, so that users may edit their selection in case of error or other issues. This example assumes a form with a xref:System.Windows.Forms.FolderBrowserDialog component and a xref:System.Windows.Forms.TextBox control.

    Public Sub ChooseFolder()
        If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub
    
    public void ChooseFolder()
    {
        if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = folderBrowserDialog1.SelectedPath;
        }
    }
    
    public:
       void ChooseFolder()
       {
          if (folderBrowserDialog1->ShowDialog() == DialogResult::OK)
          {
             textBox1->Text = folderBrowserDialog1->SelectedPath;
          }
       }
    

    Important

    To use this class, your assembly requires a privilege level granted by the xref:System.Security.Permissions.FileIOPermissionAttribute.PathDiscovery%2A property, which is part of the xref:System.Security.Permissions.FileIOPermissionAccess enumeration. If you are running in a partial-trust context, the process might throw an exception because of insufficient privileges. For more information, see Code Access Security Basics.

For information on how to save files, see How to: Save Files Using the SaveFileDialog Component.

See also