Setting the initial directory of an SaveFileDialog?

Solution 1:

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

Solution 2:

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;

Solution 3:

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);
}