Setting the initial directory of an SaveFileDialog?

Viewed 64181

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?

15 Answers

I found that setting InitialDirectory to null first works around user history.

    OpenFileDialog dlgOpen = new OpenFileDialog();
    dlgOpen.InitialDirectory = null;
    dlgOpen.InitialDirectory = @"c:\user\MyPath";

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

In case anyone is looking to do this the proper way... just follow this MSDN article and everything will work properly: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.initialdirectory?view=net-5.0

And yes: this can be done dynamically... smart way to keep track is to use a textbox control as a "last path" reference... mind you most interfaces will already be showing a path textbox on the form, in which case, just use that one... note also here the fallback to MyDocuments if the folder does not exist...

Example:

Your form here is expected to have your typical Label, TextBox and "Browse" Button trio for 2 different file path lookups, but as you can see I use the same OpenFileDialog instance to setup both dialogs when their respective browse buttons are pushed:

    private void btn_KnownServersFilePath_Browse_Click(object sender, EventArgs e)
    {

        string tmppath = Path.GetDirectoryName(this.txt_Servers_TemplatePath.Text) + "\\";
        this.openFileDialog1.InitialDirectory = Directory.Exists(tmppath) ? tmppath : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        this.openFileDialog1.Filter = "excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
        this.openFileDialog1.FilterIndex = 2;
        this.openFileDialog1.RestoreDirectory = true;
        this.openFileDialog1.FileName = Path.GetFileName(this.txt_Servers_TemplatePath.Text);
        

        if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            this.txt_Servers_TemplatePath.Text = this.openFileDialog1.FileName;
    }

    private void btn_ServerDumpFilePath_Browse_Click(object sender, EventArgs e)
    {
        string tmppath = Path.GetDirectoryName(this.txt_AllOU_TemplatePath.Text) + "\\";
        this.openFileDialog1.InitialDirectory = Directory.Exists(tmppath) ? tmppath : Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        this.openFileDialog1.Filter = "AD dump files (*.csv)|*.csv|All files (*.*)|*.*";
        this.openFileDialog1.FilterIndex = 2;
        this.openFileDialog1.RestoreDirectory = true;
        this.openFileDialog1.FileName = Path.GetFileName(this.txt_AllOU_TemplatePath.Text);

        if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            this.txt_AllOU_TemplatePath.Text = this.openFileDialog1.FileName;
    }
Related