How to save last folder in openFileDialog?

前端 未结 8 1954
刺人心
刺人心 2021-02-14 00:51

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

OpenFileDialog openFileDialog1 = new Ope         


        
相关标签:
8条回答
  • 2021-02-14 01:10

    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));
            }
        }
    
    0 讨论(0)
  • 2021-02-14 01:13

    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;
    
    0 讨论(0)
  • 2021-02-14 01:21

    After changing Settings you have to call

    Settings.Default.Save();
    

    and before you open the OpenFileDialog you set

    openFileDialog1.InitialDirectory = Settings.Default.acc_path;
    
    0 讨论(0)
  • 2021-02-14 01:22

    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)

    0 讨论(0)
  • 2021-02-14 01:26

    This is the easiest way: FileDialog.RestoreDirectory.

    0 讨论(0)
  • 2021-02-14 01:28

    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();
    }
    
    0 讨论(0)
提交回复
热议问题