28

I've read about the property on MSDN, but I still don't understand what it does.

Gets or sets a value indicating whether the dialog box restores the current directory before closing.

What exactly does that mean? What does 'restoring' the current directory actually do? Thanks for the help.

Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479

5 Answers5

30

IIRC, in windows XP when you press Save on a SaveFileDialog (or Open on a OpenFileDialog) the directory where the file is saved (or opened), is set as the new current working directory (the one in Environment.CurrentDirectory).

By setting FileDialog.RestoreDirectory = true, when you close the FileDialog the original working directory is restored.

In Windows Vista/Seven the behavior is always as FileDialog.RestoreDirectory = true (yes, even if you set it to false...).

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • @linquize: No idea, but I bet it's like Windows 7. – digEmAll Sep 14 '12 at 07:05
  • 8
    I think a lot of people (like my former self) assume that this property will _magically_ persist the "last viewed directory" for their users due to how vague the documentation is. The MSDN docs should link here. – chrnola Mar 05 '14 at 16:34
  • 1
    This feature is not implemented for the WPF OpenFileDialog – BeanFlicker May 10 '16 at 16:32
7

An annoyig quirk of the original implementation was that after you made your selection the current directory of your app changed to that folder, meaning that if you expected File operations to be relative to your app folder, it now failed.

This meant it was common practice to add code to capture the current folder before using he dialog, and restore it afterwards.

This was fixed, so it can do this for you, but changing the default behaviour might have broken code tha relied on the quirk. So you need to enable it manually via this property.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    I have no idea what the sentence `after you made your selection the current directory of your app changed to that folder` could mean. Would you mind rephrasing it? – Evgeniy Berezovsky Jun 04 '15 at 03:49
  • It means when the user opens a file, the whole application's current directory is changed to that so if the code elsewhere tries to open some internal file that it expects to be in the application folder, without explicitly specifying that folder, it won't be able to find it. – user1318499 Oct 01 '18 at 18:24
  • Thanks for the last paragraph, which the top answer doesn't have. – person27 May 16 '20 at 16:13
0

Here, I am talking about WinForms FileDialog only, WPF FileDialog may have different behaviour.

For Win XP, the current directory is changed during the use of dialog.

For multi-threaded application, this must be aware. It is easy to proof by running a loop in a new thread to check if current directory is changed during the use of dialog, when switching to another folder.

For Win 7 / mono, the current directory is not changed during or after using the dialog.

Therefore Win 7 / mono: RestoreDirectory property is not needed.

linquize
  • 19,828
  • 10
  • 59
  • 83
-1

When you can select a directory when using the dialog box, next use of the dialog will open in the last used directory. Using this option will restore the original directory.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
-1

If it's set to true the file dialog will always open the default file path (InitialDirectory property).

jbachman
  • 249
  • 2
  • 6
  • Are you sure about that? Do you have a source for that? The official WinForms documentation definitely talks about closing behavior. NOT opening behavior. – Kissaki Jan 11 '22 at 13:34