Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

FolderBrowserDialog.SelectedPath Property

Gets or sets the path selected by the user.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

[LocalizableAttribute(true)] 
public string SelectedPath { get; set; }
/** @property */
public String get_SelectedPath ()

/** @property */
public void set_SelectedPath (String value)

public function get SelectedPath () : String

public function set SelectedPath (value : String)

Not applicable.

Property Value

The path of the folder first selected in the dialog box or the last folder selected by the user. The default is an empty string ("").

If the SelectedPath property is set before showing the dialog box, the folder with this path will be the selected folder, as long as SelectedPath is set to an absolute path that is a subfolder of RootFolder (or more accurately, points to a subfolder of the shell namespace represented by RootFolder).

If the ShowDialog returns OK, meaning the user clicked the OK button, the SelectedPath property will return a string containing the path to the selected folder. If ShowDialog returns Cancel, meaning the user canceled out of the dialog box, this property will have the same value that it had prior to displaying the dialog box. If the user selects a folder that does not have a physical path (for example, My Computer), the OK button on the dialog box will be disabled.

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.
    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();
            } 
        }
    }
}

// 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.
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
import System.IO.*;

public class FolderBrowserDialogExampleForm extends 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 boolean fileOpened = false;

    // The main entry point for the application.
    public static void main(String[] args)
    {
        Application.Run(new FolderBrowserDialogExampleForm());
    } //main

    // 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.get_MenuItems().Add(this.fileMenuItem);
        this.fileMenuItem.get_MenuItems().AddRange(
            new System.Windows.Forms.MenuItem[] { this.openMenuItem, 
            this.closeMenuItem, this.folderMenuItem });
        this.fileMenuItem.set_Text("File");

        this.openMenuItem.set_Text("Open...");
        this.openMenuItem.add_Click(new System.EventHandler(
            this.openMenuItem_Click));

        this.folderMenuItem.set_Text("Select Directory...");
        this.folderMenuItem.add_Click(new System.EventHandler(
            this.folderMenuItem_Click));

        this.closeMenuItem.set_Text("Close");
        this.closeMenuItem.add_Click(new System.EventHandler(
            this.closeMenuItem_Click));
        this.closeMenuItem.set_Enabled(false);

        this.openFileDialog1.set_DefaultExt("rtf");
        this.openFileDialog1.set_Filter("rtf files (*.rtf)|*.rtf");
        // Set the help text description for the FolderBrowserDialog.
        this.folderBrowserDialog1.set_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.set_ShowNewFolderButton(false);
        // Default to the My Documents folder.
        this.folderBrowserDialog1.set_RootFolder(
            Environment.SpecialFolder.Personal);

        this.richTextBox1.set_AcceptsTab(true);
        this.richTextBox1.set_Location(new System.Drawing.Point(8, 8));
        this.richTextBox1.set_Size(new System.Drawing.Size(280, 344));
        this.richTextBox1.set_Anchor(AnchorStyles.Top | AnchorStyles.Left 
            | AnchorStyles.Bottom | AnchorStyles.Right);

        this.set_ClientSize(new System.Drawing.Size(296, 360));
        this.get_Controls().Add(this.richTextBox1);
        this.set_Menu(this.mainMenu1);
        this.set_Text("RTF Document Browser");
    } //FolderBrowserDialogExampleForm

    // 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.set_InitialDirectory(
                folderBrowserDialog1.get_SelectedPath());
            openFileDialog1.set_FileName(null);
        }
        // Display the openFile dialog.
        DialogResult result = openFileDialog1.ShowDialog();
        // OK button was pressed.
        if (result.Equals(get_DialogResult().OK)) {
            openFileName = openFileDialog1.get_FileName();
            try {
                // Output the requested file in richTextBox1.
                Stream s = openFileDialog1.OpenFile();
                richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
                s.Close();
                fileOpened = true;
            }
            catch (System.Exception exp) {
                MessageBox.Show("An error occurred while attempting to " 
                    + "load the file. The error is:" 
                    + System.Environment.get_NewLine() 
                    + exp.ToString() + System.Environment.get_NewLine());
                fileOpened = false;
            }
            Invalidate();
            closeMenuItem.set_Enabled(fileOpened);
        }
        // Cancel button was pressed.
        else {
            if (result.Equals(get_DialogResult().Cancel)) {
                return;
            }
        }
    } //openMenuItem_Click

    // Close the current file.
    private void closeMenuItem_Click(Object sender, System.EventArgs e)
    {
        richTextBox1.set_Text("");
        fileOpened = false;
        closeMenuItem.set_Enabled(false);
    } //closeMenuItem_Click

    // 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.Equals(get_DialogResult().OK)) {
            folderName = folderBrowserDialog1.get_SelectedPath();
            if (!(fileOpened)) {
                // No file is opened, bring up openFileDialog in selected path.
                openFileDialog1.set_InitialDirectory(folderName);
                openFileDialog1.set_FileName(null);
                openMenuItem.PerformClick();
            }
        }
    } //folderMenuItem_Click
} //FolderBrowserDialogExampleForm

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.