Setting the initial directory of an SaveFileDialog?
I too have tried different "solutions" found in different places, but none of them seem to work as soon as there is an MRU list entry in the registry :/ But here is my own simple workaround…
Instead of setting the dialog's InitialDirectory
property, set the FileName
property to your path, but combined with the selected Filter
, e.g.:
dialog.FileName = Path.Combine(myPath, "*.*");
I have no idea why this works, but I was finally able to get it working for me.
I found that if I gave the full path, it would not work, but if I put that full path inside of Path.GetFullPath(), then it would work. Looking at the before and after values show them being the same, but it would consistently not work without it, and work with it.
//does not work
OpenFileDialog dlgOpen = new OpenFileDialog();
string initPath = Path.GetTempPath() + @"\FQUL";
dlgOpen.InitialDirectory = initPath;
dlgOpen.RestoreDirectory = true;
//works
OpenFileDialog dlgOpen = new OpenFileDialog();
string initPath = Path.GetTempPath() + @"\FQUL";
dlgOpen.InitialDirectory = Path.GetFullPath(initPath);
dlgOpen.RestoreDirectory = true;
Make sure to check that the directory path exists before setting the Initial directory property. Create the directory if it does not exist. ie
if (!Directory.Exists(FooDirectory))
{
Directory.CreateDirectory(FooDirectory);
}
You need to set the RestoreDirectory
to true
as well as the InitialDirectory
property.