How to save last folder in openFileDialog?

2019-03-26 07:41发布

问题:

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

回答1:

This is the easiest way: FileDialog.RestoreDirectory.



回答2:

After changing Settings you have to call

Settings.Default.Save();

and before you open the OpenFileDialog you set

openFileDialog1.InitialDirectory = Settings.Default.acc_path;


回答3:

I think it would be enough for you to use SetCurrentDirectory to ste the current directory for the OS. So on the next dialog opening it would pick that path.

Or simply save path into some variable of your application and use
FileDialog.InitialDirectory property.



回答4:

The following is all you need to make sure that OpenFileDialog will open at the directory the user last selected, during the lifetime off your application.

OpenFileDialog OpenFile = new OpenFileDialog();
OpenFile.RestoreDirectory = false;


回答5:

You can use the InitialDirectory property : http://msdn.microsoft.com/fr-fr/library/system.windows.forms.filedialog.initialdirectory.aspx

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.InitialDirectory = previousPath;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    previousPath = Path.GetDirectoryName(openFileDialog1.FileName);
    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();
}


回答6:

I find that all you have to do is NOT set the initial directory and the dialog box remembers your last save/open location. This remembers even after the application is closed and reopened. Try this code with the initial directory commented out. Many of the suggestions above will also work but if you are not looking for additional functionality this is all you have to do.

    private void button1_Click(object sender, EventArgs e)
    {
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        //openFileDialog1.InitialDirectory = "c:\\";
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        // Insert code to read the stream here.
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }
    }


回答7:

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


回答8:

if your using

Dim myFileDlog As New OpenFileDialog()

then you can use this to restore the last directory

myFileDlog.RestoreDirectory = True

and this to not

myFileDlog.RestoreDirectory = False

(in VB.NET)