tom greene tom greene - 4 months ago 53
C# Question

Setting the initial directory of an SaveFileDialog?

I'd like a SaveFileDialog with the following behavior:

  • The first time you open it, it goes to "My Documents".

  • Afterwards, it goes to the last selected folder.
    What's the best way to accomplish this?

If I don't set the InitialDirectory, it goes to the exe's directory - which is not what I want. It rememebers the last selected directory though - even between executions.

If I set the InitialDirectory, it does not remember the last selected directory. Of course, I could save the last selected directory in the registry :( but I am looking for a better solution.

SaveFileDialog dialog = new SaveFileDialog();
//??? dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

Any advice?


You need to set the RestoreDirectory to true as well as the InitialDirectory property.