Search code examples
c#openfiledialog

How to save last folder in openFileDialog?


How do I make my application store the last path opened in openFileDialog and after new opening restore it?

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    acc_path = openFileDialog1.FileName;
    Settings.Default.acc_path = acc_path;

    foreach (string s in File.ReadAllLines(openFileDialog1.FileName))
    {
        accs.Enqueue(s);
    }
    label2.Text = accs.Count.ToString();
}

Solution

  • This is the easiest way: FileDialog.RestoreDirectory.