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
If you use forward slash anywhere in your path, InitialDirectory does not work. Make sure they are converted to back slashes
You need to set the RestoreDirectory to true
as well as the InitialDirectory property.
None of the provided solutions worked for me sadly.
In addition to the OP specifications, I wanted the program to remember the last save location between runs. For this, in the Visual Studios Solution Explorer under ProjectName -> Properties -> Settings.settings
, I setup the following property:
Because I am keeping the SaveFileDialog
around for the duration of the program's running, I instantiate at the start. Then in the Command
for bringing up the dialog:
if (string.IsNullOrEmpty(Settings.Default.PreviousPath))
{
this.saveDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
else
{
this.saveDialog.InitialDirectory = Settings.Default.PreviousPath;
}
this.saveDialog.FileName = "Default_File_Name";
bool result = this.saveDialog.ShowDialog() ?? false;
if (result)
{
Settings.Default.PreviousPath = Path.GetDirectoryName(this.saveDialog.FileName);
Settings.Default.Save();
// Code writing to the new file...
}
This gives the behavior:
use the saveFileDialog.InitialDirectory
property as follows:
Stream stream;
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "dat files (*.dat)|*.dat|All files (*.*)|*.*";
saveFileDialog.FilterIndex = 1;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.InitialDirectory = @"D:\Data\...\foldername\";
saveFileDialog.ShowDialog();
if (!saveFileDialog.FileName.Equals(string.Empty))
Console.WriteLine(saveFileDialog.FileName);
I found that setting InitialDirectory
to null
first works around user history.
OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.InitialDirectory = null;
dlgOpen.InitialDirectory = @"c:\user\MyPath";
I did some testing with .NET 2.0 and it seems if I set FileName to anything other than a literal string it doesn't work. When I use a method or accesstor to set the property the initial directory is ignored.