5

The file dialog has to open the last directory location that was used before it was shut down, but I have no idea how to do this. My colleague only shows me the example of word, when you click "file" it shows the last used files, he told me to use a register or an INI file, which I have never used before.

Here is the code I am using:

string f_sOudeLocatie = @"D:\path\is\classified";

private void btBrowse_Click(object sender, EventArgs e)
{
    OpenFileDialog fdlg = new OpenFileDialog();
    fdlg.Title = "Zoek de CSV file";
    fdlg.InitialDirectory = f_sOudeLocatie;
    fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
    fdlg.FilterIndex = 1;
    fdlg.RestoreDirectory = true;
    if (fdlg.ShowDialog() == DialogResult.OK)
    {
        tbGekozenBestand.Text = fdlg.FileName;
        tbVeranderNaamIn.Text = Path.GetDirectoryName(fdlg.FileName);
        f_sOudeLocatie = Path.GetDirectoryName(fdlg.FileName);
        f_sSourceFileName = fdlg.FileName;
        f_sDestFileName = Path.GetFileName(Path.GetDirectoryName(fdlg.FileName)) + ".csv";
        btOpslaan.Enabled = true;
        tbVeranderNaamIn.ReadOnly = false;
    }
}
C-Pound Guru
  • 15,967
  • 6
  • 46
  • 67
Cas Nouwens
  • 395
  • 1
  • 5
  • 25
  • 1
    Try run your application a couple of times. It should be native behaviour that Windows explorer windows open is the last directory where there was opened some file. I works for me. – Ondrej Janacek Sep 03 '13 at 07:06
  • 1
    I think this should work because `InitialDirectory` is set to `f_sOutdeLocatie` which is saved everytime user clicks `OK`. – King King Sep 03 '13 at 07:06
  • 5
    What isn't working? Have you asked your colleague? – Sayse Sep 03 '13 at 07:06
  • 1
    Title wouldn't be *how to store a value and retrieve it?* No really related to `OpenFileDialog` I think... – Chris Sep 03 '13 at 07:29

3 Answers3

10

if you'll create the OpenFileDialog outside the button click event it should remember the last folder you've been

string f_sOudeLocatie = @"D:\path\is\classified";
OpenFileDialog fdlg = new OpenFileDialog();

public Form1()
{
    InitializeComponent();
    fdlg.Title = "Zoek de CSV file";
    fdlg.InitialDirectory = f_sOudeLocatie;
    fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
    fdlg.FilterIndex = 1;
    fdlg.RestoreDirectory = true;
}
private void btBrowse_Click(object sender, EventArgs e)
{
    if (fdlg.ShowDialog() == DialogResult.OK)
    {
        fdlg.InitialDirectory = fdlg.FileName.Remove(fdlg.FileName.LastIndexOf("\\"));// THIS LINE IS IMPORTENT

        tbGekozenBestand.Text = fdlg.FileName;
        tbVeranderNaamIn.Text = Path.GetDirectoryName(fdlg.FileName);
        f_sOudeLocatie = Path.GetDirectoryName(fdlg.FileName);
        f_sSourceFileName = fdlg.FileName;
        f_sDestFileName = Path.GetFileName( Path.GetDirectoryName(fdlg.FileName) ) + ".csv";
        btOpslaan.Enabled = true;
        tbVeranderNaamIn.ReadOnly = false;
    }
}
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • Or you may use `Path` static methods: `Path.GetDirectoryName(fdlg.FileName)` instead of recode it. – Orace Jun 19 '20 at 12:02
3

You need to set

fdlg.RestoreDirectory = false;

Reason:

RestoreDirectory property makes sure that the value in Environment.CurrentDirectory will be reset before the OpenFileDialog closes. If RestoreDirectory is set to false, then Environment.CurrentDirectory will be set to whatever directory the OpenFileDialog was last open to. As explained here

Ehsan
  • 31,833
  • 6
  • 56
  • 65
1

You can use registry to store the last directory location. And each time you open the file dialogue, get the value from the registry and set as the default location. When it closed store the location back to registry.

This code project article explains you well about reading and writing to registry ReadWriteDeleteFromRegistry

If you choose to use INI file, some search will give you examples of how to read and write from INI file

Peter K John
  • 101
  • 4