Setting the initial directory of an SaveFileDialog?

62,124

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

Solution 4

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, "*.*");

Solution 5

The suggested workarounds didn't work for me, so after finding How does WPF OpenFileDialog track directory of last opened file? I implemented:

public static void SetInitialDirectory(this FileDialog dlg, string fileExtension, string initialDirectory)
        {
            // RestoreDirectory doesn't seem to be implemented - https://stackoverflow.com/questions/11144770/how-does-wpf-openfiledialog-track-directory-of-last-opened-file
            // so manually only set InitialDirectory if nothing is stored
            try
            {
                var mru = @"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU\" + fileExtension;
                var rk = Registry.CurrentUser.OpenSubKey(mru);
                if (rk == null)
                {
                    dlg.InitialDirectory = initialDirectory;
                }
            }
            catch (Exception)
            {
                // SecurityException, ObjectDisposedException => allow default behaviour
            }
        }

This will use the provided initialDirectory if the dialog has not been used before for this file extension. Once the dialog has been used, it reverts to the default behaviour of remembering the previous directory.

Share:
62,124
tom greene
Author by

tom greene

Updated on April 09, 2021

Comments

  • tom greene
    tom greene almost 3 years

    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?

  • SLA80
    SLA80 about 14 years
    Also see this thread, where programmer says that setting "RestoreDirectory" to true doesn't help: discuss.joelonsoftware.com/default.asp?dotnet.12.424113.2
  • Patrick Klug
    Patrick Klug over 13 years
    really? Doesn't work for me. It just always navigates to the InitialDirectory?
  • Jeffrey Harmon
    Jeffrey Harmon over 11 years
    I had the same issue, but was finally able to get past it (see my answer to this question for an example). For some reason, passing the full path into Path.GetFullPath(), and then setting InitialDirectory to it works.
  • Hagelt18
    Hagelt18 over 9 years
    You can just call Directory.CreateDirectory(FooDirectory) and it will not throw an error if the directory already exists.
  • danio
    danio about 8 years
    This didn't help for me (Windows 7, 64-bit, .net framework 4.5.1)
  • danio
    danio about 8 years
    This didn't help for me - it just opened a dialog with the full path and filename in the file name field, but the directory shown was the one remembered (Windows 7, 64-bit, .net framework 4.5.1)
  • vapcguy
    vapcguy almost 7 years
    Didn't work setting it to null to avoid user history issue for me, on OpenSaveDialog(), where I had deleted the previous save location. Still have error that the location doesn't exist and the old place is still the place it tries to go.
  • vapcguy
    vapcguy almost 7 years
    +1 Even though it didn't fix my problem directly, it does work if you have a directory you deleted and no longer want to be the initial directory, but it's stuck in the MRU, IF you actually have it go through the dlg.InitialDirectory = initialDirectory; part to set it to something else, null or not. Mine skipped it during debug using jpg as the fileExtension since I had a JPG file where I inadvertently created a directory with that file's name & saved the file inside, then deleted the folder to cause my problem. Somehow just backing up the debugger to run that line and set a path worked.
  • vapcguy
    vapcguy almost 7 years
    And actually, what I found was I wasn't using an environmental path variable correctly, so it wasn't saving the correct path as the initialDirectory. Setting that variable manually through the debugger helped me to figure out what it was trying to be set to, and to set it manually to something to ensure it even could be set. Once I figured out what the problem was, I went a different way, since I could always null it and set it anew, but this helped so I could have a function to pass what I thought was the correct path to it, just to find out where I actually messed up. :)
  • vapcguy
    vapcguy almost 7 years
    I figured out that my issue was in my path. Above code will work fine. I was incorrectly using an environmental path variable (%USERPROFILE%), causing my path to be incorrect, so the dialog kept just using the last path it had - which was the one I had deleted. So as long as your path is correct, this will set it. :)
  • Dog Lover
    Dog Lover over 6 years
    @danio I have Windows 7 64-bit and this worked for me. My application uses .NET 4.