Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the initial directory of an SaveFileDialog?

Tags:

c#

.net

wpf

winapi

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);       dialog.ShowDialog(); 

Any advice?

like image 368
tom greene Avatar asked Jul 24 '09 00:07

tom greene


People also ask

What is the initial directory?

The InitialDirectory property is typically set using one of the following sources: A path that was previously used in the program, perhaps retained from the last directory or file operation. A path read from a persistent source, such as an application setting, a Registry or a string resource in the application.

How we can save a file using SaveFileDialog control with example?

Use the SaveFileDialog component's OpenFile method to save the file. This method gives you a Stream object you can write to. The example below uses the DialogResult property to get the name of the file, and the OpenFile method to save the file. The OpenFile method gives you a stream to write the file to.

What is the use of SaveFileDialog control?

The SaveFileDialog control prompts the user to select a location for saving a file and allows the user to specify the name of the file to save data. The SaveFileDialog control class inherits from the abstract class FileDialog.


1 Answers

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

like image 140
Andrew Hare Avatar answered Sep 19 '22 00:09

Andrew Hare