Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 758
Kenji Avatar asked Apr 18 '13 08:04

Kenji


People also ask

How do I get the selected file from OpenFileDialog box?

OpenFileDialog component opens the Windows dialog box for browsing and selecting files. To open and read the selected files, you can use the OpenFileDialog. OpenFile method, or create an instance of the System. IO.

How do I select multiple files in OpenFileDialog in VB net?

Solution 1. You would have to create your own custom control to do that. By default OpenFileDialog does not support multi select of files from different folders.


1 Answers

I know this is a bit of an old thread, but I was not able to find a solution I liked to this same question so I developed my own. I did this in WPF but it should work almost the same in Winforms.

Essentially, I use an app.config file to store my programs last path.

When my program starts I read the config file and save to a global variable. Below is a class and function I call when my program starts.

public static class Statics
{
    public static string CurrentBrowsePath { get; set; }

    public static void initialization()
    {
        ConfigurationManager.RefreshSection("appSettings");
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

        CurrentBrowsePath = ConfigurationManager.AppSettings["lastfolder"];
    }
}

Next I have a button that opens the file browse dialog and sets the InitialDirectory property to what was stored in the config file. Hope this helps any one googling.

    private void browse_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog open_files_dialog = new OpenFileDialog();
        open_files_dialog.Multiselect = true;
        open_files_dialog.Filter = "Image files|*.jpg;*.jpeg;*.png";
        open_files_dialog.InitialDirectory = Statics.CurrentBrowsePath;

        try
        {
            bool? dialog_result = open_files_dialog.ShowDialog();

            if (dialog_result.HasValue && dialog_result.Value)
            {
                string[] Selected_Files = open_files_dialog.FileNames;

                if (Selected_Files.Length > 0)
                {
                    ConfigWriter.Update("lastfolder", System.IO.Path.GetDirectoryName(Selected_Files[0]));
                }

                // Place code here to do what you want to do with the selected files.
            }
        }
        catch (Exception Ex)
        {
            MessageBox.Show("File Browse Error: " + Environment.NewLine + Convert.ToString(Ex));
        }
    }
like image 178
David Bentley Avatar answered Oct 08 '22 11:10

David Bentley