如何在openFileDialog中保存最后一个文件夹?

如何使我的应用程序存储在openFileDialog打开的最后一个路径以及新的打开恢复后呢?

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

这是最简单的方法: FileDialog.RestoreDirectory 。

更改设置后,您必须致电

 Settings.Default.Save(); 

在打开你设置的OpenFileDialog之前

 openFileDialog1.InitialDirectory = Settings.Default.acc_path; 

我认为使用SetCurrentDirectory来修改操作系统的当前目录就足够了。 所以在下一个对话框打开时,它将选择该路径。

或者只是将路径保存到应用程序的某个变量中并使用
FileDialog.InitialDirectory属性。

以下是您在应用程序生命周期内确保OpenFileDialog将在用户上次选择的目录中打开所需的全部内容。

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

您可以使用InitialDirectory属性: 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(); } 

我发现你所要做的就是不设置初始目录,对话框会记住你上次保存/打开的位置。 即使应用程序关闭并重新打开,这也会记住。 尝试使用此代码并将初始目录注释掉。 上面的许多建议也可以使用,但如果您不想寻找其他function,这就是您需要做的。

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

我知道这是一个旧线程,但我无法找到我喜欢的同一个问题的解决方案,所以我开发了自己的。 我在WPF中这样做但它在Winforms中的工作方式几乎相同。

基本上,我使用app.config文件存储我的程序的最后路径。

当我的程序启动时,我读取配置文件并保存到全局变量。 下面是我程序启动时调用的类和函数。

 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"]; } } 

接下来,我有一个按钮,用于打开文件浏览对话框,并将InitialDirectory属性设置为存储在配置文件中的属性。 希望这有助于任何一个谷歌搜索。

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

如果你使用

 Dim myFileDlog As New OpenFileDialog() 

然后你可以用它来恢复最后一个目录

 myFileDlog.RestoreDirectory = True 

这不是

 myFileDlog.RestoreDirectory = False 

(在VB.NET中)