Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
    }
}
like image 643
Cas Nouwens Avatar asked Sep 03 '13 07:09

Cas Nouwens


Video Answer


1 Answers

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;
    }
}
like image 64
No Idea For Name Avatar answered Sep 23 '22 13:09

No Idea For Name