Cas Nouwens
Cas Nouwens

Reputation: 405

Initial directory for OpenFileDialog

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

Upvotes: 5

Views: 34587

Answers (3)

Ehsan
Ehsan

Reputation: 32661

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

Upvotes: 3

Peter K John
Peter K John

Reputation: 101

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

Upvotes: 1

No Idea For Name
No Idea For Name

Reputation: 11577

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

Upvotes: 10

Related Questions