在fileopen对话框中获取最后打开的文件

在FileOpen或FileSave对话框中,当我调用它们时,它们会自动转到上一个打开的路径。 即使我关闭我的应用程序并打开它也会发生这种情况。 但是如何将该路径/文件名转换为文本框或变量?

它似乎有点受欢迎,但在Windows 7下它适用于以下方式:

OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); 

试一试,告诉我你是否需要进一步的帮助。

据推测,信息存储在注册表深处的某个地方(它由OpenFileDialog只是一个包装器的非托管控件完成)。 最简单的可能是在您的应用程序中最后一次关闭对话框时可以访问它的路径。

如上所述: FileDialog.RestoreDirectory属性实际上做了什么?

他们使用Environment.CurrentDirectory的路径。

我和Vicky有类似的问题,如下所示。 我正在使用Vista Business SP2下的Visual Basic 2008 Express Edition进行开发。

我有一个OpenFileDialog和SaveFileDialog的应用程序。 当我在第一次运行应用程序时调用OpenFileDialog时,它默认为Application上次打开/保存文件的目录。 但是,此目录不是“Environment.CurrentDirectory”,它设置为“C:\ Users \ Brian \ Documents \ Visual Studio 2008 \ Projects \ IFPM Analysis \ IFPM Analysis \ bin \ Debug”,并且不会被OpenFileDialog或SaveFileDialog。

稍后在Application中,我调用SaveFileDialog,将代码中设置的初始目录属性(.InitialDirectory)设置为默认目录。 当我随后调用OpenFileDialog时,它默认为SaveFileDialog使用的目录。 “Environment.CurrentDirectory”的值始终保持不变。

所以,我的问题是,存储OpenFileDialog和SaveFileDialog所使用的目录在哪里? 我假设它与底层的FileDialog类有关,我知道即使在Application关闭并重新启动后仍然存在。

理想情况下,我希望能够存储用户从OpenFileDialog中选择的目录,并在使用SaveFileDialog后重置它。 虽然我可以在应用程序中使用OpenFileDialog的InitialDirectory属性,但当我关闭应用程序并重新启动它时,这对我没有帮助。 遗憾的是,典型的用户操作是:

  • 开始申请
  • 用OpenFileDialog打开文件
  • 使用SaveFileDialog保存文件
  • 离开应用程序

这意味着当用户返回到应用程序时,默认目录是“错误”目录。 我知道我可以在我自己的配置文件中保存OpenFileDialog中最后使用的目录,这样它就会在Application之外保留,但是当Windows为我提供相同的function时,这似乎有点傻,如果我知道在哪里看的话!

任何帮助感激不尽!

谢谢,Brian。

最近打开的文件列表存储在2个位置:

  • 最近的文件夹:最近的文件夹通常位于C:\ Documents and Settings [Your Profile] \ Recent(Windows Vista下的路径不同),它包含最近打开的文件的快捷方式。
  • 注册表:每次在保存/打开对话框中选择文件时,文件HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU添加到HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU下的文件列表中

此方法可以帮助您获取列表:

 public static string GetLastOpenSaveFile(string extention) { RegistryKey regKey = Registry.CurrentUser; string lastUsedFolder = string.Empty; regKey = regKey.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU"); if (string.IsNullOrEmpty(extention)) extention = "html"; RegistryKey myKey = regKey.OpenSubKey(extention); if (myKey == null && regKey.GetSubKeyNames().Length > 0) myKey = regKey.OpenSubKey(regKey.GetSubKeyNames()[regKey.GetSubKeyNames().Length - 2]); if (myKey != null) { string[] names = myKey.GetValueNames(); if (names != null && names.Length > 0) { lastUsedFolder = (string)myKey.GetValue(names[names.Length - 2]); } } return lastUsedFolder; } 

成功! 约尔丹