Toby
Toby

Reputation: 10144

Make FileDialogs start from given directory but then from last used

I have a form that has an OpenFileDialog and a SaveFileDialog declared at class scope:

    private OpenFileDialog OpenDialog = new OpenFileDialog()
    {
        Title = "Open",
        AddExtension = true,
        DefaultExt = "json",
        Filter = "JSON Files (*.json)|*.json",
        CheckFileExists = true,
        CheckPathExists = true,
        DereferenceLinks = true,
        Multiselect = false,
        ShowReadOnly = false,
        ValidateNames = true,
        RestoreDirectory = false
    };

    SaveFileDialog SaveDialog = new SaveFileDialog()
    {
        Title = "Save",
        AddExtension = true,
        CheckPathExists = true,
        CreatePrompt = false,
        DefaultExt = "json",
        Filter = "JSON Files (*.json)|*.json",
        RestoreDirectory = false
    };

They are then used in a event handlers like so:

    private void openToolStripButton_Click(object sender, EventArgs e)
    {
        if (OpenDialog.ShowDialog(this) != DialogResult.Cancel)
        {
            //...

I would like to set these so that the first time a user opens the application the directory that the dialogs open at is a predemined one. Thereafter however, should the user open the dialogs again they should open to whatever directory the user last used them at.

I have been dickering around with the RestoreDirectory and InitialDirectory properties in the main form's Load event but have not been able to achieve this. Is this possible using the Dialogs' standard properties or methods?

Upvotes: 0

Views: 50

Answers (1)

rory.ap
rory.ap

Reputation: 35260

Most likely you will need to persist this information, e.g. in the Windows registry. This would be an example of a user setting, i.e. one that is stored separately for each user who logs onto the machine. The first time the dialog is open, the setting would be absent and thus would default to your initial directory; then, you store the chosen value and use that for subsequent dialogs.

Upvotes: 2

Related Questions