Get Browse Folder dialog box in C #

Source: Internet
Author: User
dialog box We know, C # there is a selection of File dialog box control, but there is no folder to select the dialog box control, many people in order to choose a folder to use the TreeView class to deal with, in fact, in C # without so much trouble, we can use C # Subclass of the Foldernameeditor class FolderBrowser class to implement the ability to get Browse Folders dialog boxes.

Let's see how it's going to be implemented.

Create a new WinForm project first, and then create a new class file (File->addnewitem->class).

Because Foldernameeditor is under the System.Windows.Forms.Design namespace, this namespace is located in the dynamic link library System.Design.dll, so we first add a new reference to the solution (reference) SYSTEM.D Esign. And then add it in your code.
Using System.Windows.Forms.Design;


Next, we create a new Folderdialog class that inherits from the Foldernameeditor class and creates a new instance of the FolderBrowser class in this class. The Displaydialog (string description) function sets the properties of the instance fdialog and displays the Browse Folder dialog box. To get the path to the folder, set a path property that returns the paths to the selected folder.

The code is as follows:
public class Folderdialog:foldernameeditor {Foldernameeditor.folderbrowser fdialog = new System.Windows.Forms.Design.FolderNameEditor.FolderBrowser (); Public Folderdialog () {} public DialogResult Displaydialog () {return Displaydialog ("Please select a folder");} public DialogResult Di Splaydialog (String description) {fdialog.description = description; return Fdialog.showdialog ();} public string Path {g ET {return fdialog.directorypath;}} ~folderdialog () {fdialog.dispose ();}}


With this new class, you can display the Browse Folder dialog box.

Add a TextBox control to the Form1 and add the following code in the Form1 constructor:
Folderdialog Openfolder = new Folderdialog (); if (Openfolder.displaydialog () ==dialogresult.ok) textbox1.text=openfolder.path.tostring (); Else textbox1.text= "You have not selected directory";


Running this project will pop up the following Browse Folder dialog box, is it the same as Windows itself?



Click OK to show the path of the folder you selected, click Cancel to show that you did not select the directory.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.