【问题标题】:How to save last folder in openFileDialog?如何在openFileDialog中保存最后一个文件夹?
【发布时间】:2013-04-18 08:46:04
【问题描述】:

如何使我的应用商店成为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();
}

【问题讨论】:

    标签: c# openfiledialog


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      我知道这是一个旧线程,但我无法找到我喜欢的解决方案来解决同样的问题,所以我开发了自己的解决方案。我在 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));
              }
          }
      

      【讨论】:

        【解决方案3】:

        更改设置后,您必须调用

        Settings.Default.Save();
        

        在你打开你设置的 OpenFileDialog 之前

        openFileDialog1.InitialDirectory = Settings.Default.acc_path;
        

        【讨论】:

        • 我个人喜欢这种方法,因为我不会在多个表单中重复使用相同的文件选择器实例。
        【解决方案4】:

        我发现您所要做的就是不设置初始目录,对话框会记住您上次保存/打开的位置。即使在应用程序关闭并重新打开后,这也会记住。在注释掉初始目录的情况下尝试此代码。上面的许多建议也适用,但如果您不是在寻找额外的功能,这就是您所要做的。

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

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

          • 也不要设置 InitialDirectory 属性。
          • ...但我认为您的意思是:OpenFile.RestoreDirectory = true;
          • OpenFile.RestoreDirectory = true 和 OpenFile.RestoreDirectory = false,两者都可以工作,无需设置 InitialDirectory 属性
          【解决方案6】:

          我认为您使用SetCurrentDirectory 来管理操作系统的当前目录就足够了。因此,在下一个对话框打开时,它会选择该路径。

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

          【讨论】:

            【解决方案7】:

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

            【讨论】:

              【解决方案8】:

              如果你使用

              Dim myFileDlog As New OpenFileDialog()
              

              那么你可以用它来恢复上一个目录

              myFileDlog.RestoreDirectory = True
              

              这不是

              myFileDlog.RestoreDirectory = False
              

              (在 VB.NET 中)

              【讨论】:

                猜你喜欢
                • 2020-08-17
                • 1970-01-01
                • 1970-01-01
                • 2023-01-10
                • 1970-01-01
                • 2021-12-25
                • 1970-01-01
                • 1970-01-01
                • 2012-06-19
                相关资源
                最近更新 更多