FileDialog.RestoreDirectory FileDialog.RestoreDirectory FileDialog.RestoreDirectory FileDialog.RestoreDirectory Property


ダイアログ ボックスを閉じる前に、ディレクトリを以前選択されていたディレクトリに復元するかどうかを示す値を取得または設定します。Gets or sets a value indicating whether the dialog box restores the directory to the previously selected directory before closing.

 property bool RestoreDirectory { bool get(); void set(bool value); };
public bool RestoreDirectory { get; set; }
member this.RestoreDirectory : bool with get, set
Public Property RestoreDirectory As Boolean


ファイルの検索中にユーザーがディレクトリを変更したときに、ダイアログ ボックスが現在のディレクトリを以前選択されていたディレクトリに復元する場合は true。それ以外の場合は falsetrue 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. 既定値は false です。The default value is false.

次のコード例ではOpenFileDialog 、のFileDialog実装を使用して、プロパティの作成、設定、およびダイアログボックスの表示について説明します。The following code example uses the OpenFileDialog implementation of FileDialog and illustrates creating, setting of properties, and showing the dialog box. この例ではRestoreDirectory 、プロパティを使用して、ダイアログボックスを閉じたときに、以前に選択したディレクトリが復元されるようにしています。The example uses the RestoreDirectory property to ensure that the previously selected directory is restored when the dialog box is closed. この例では、がButton配置されたフォームSystem.IOと、それに追加された名前空間が必要です。The example requires a form with a Button placed on it and the System.IO namespace added to it.

   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
      Stream^ myStream;
      OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

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

      if ( openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK )
         if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
            // Insert code to read the stream here.
var fileContent = string.Empty;
var filePath = string.Empty;

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

    if (openFileDialog.ShowDialog() == DialogResult.OK)
        //Get the path of specified file
        filePath = openFileDialog.FileName;

        //Read the contents of the file into a stream
        var fileStream = openFileDialog.OpenFile();

        using (StreamReader reader = new StreamReader(fileStream))
            fileContent = reader.ReadToEnd();

MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim myStream As Stream = Nothing
    Dim openFileDialog1 As New OpenFileDialog()

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

    If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            myStream = openFileDialog1.OpenFile()
            If (myStream IsNot Nothing) Then
                ' Insert code to read the stream here.
            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (myStream IsNot Nothing) Then
            End If
        End Try
    End If
End Sub


プロパティを設定する場合は。to set the property. Unrestricted (関連する列挙体)Associated enumeration: Unrestricted.