RestoreDirectory Property

FileDialog.RestoreDirectory Property

 

Gets or sets a value indicating whether the dialog box restores the directory to the previously selected directory before closing.

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

public bool RestoreDirectory { get; set; }

Property Value

Type: System.Boolean

true if the dialog box restores the current directory to the previously selected directory if the user changed the directory while searching for files; otherwise, false. The default value is false.

The following code example uses the OpenFileDialog implementation of FileDialog and illustrates creating, setting of properties, and showing the dialog box. The example uses the RestoreDirectory property to ensure that the previously selected directory is restored when the dialog box is closed. The example requires a form with a Button placed on it and the System.IO namespace added to it.

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Insert code to read the stream here.
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
        }
    }
}

FileIOPermission

to set the property. Associated enumeration: PermissionState.Unrestricted.

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft