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开发者_如何学C.
What exactly does that mean? What does 'restoring' the current directory actually do? Thanks for the help.
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...).
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.
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.
If it's set to true the file dialog will always open the default file path (InitialDirectory property).
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.
精彩评论