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?
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.
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.
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.
You need to set the RestoreDirectory
to true
as well as the InitialDirectory
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With