【问题标题】:How to make the OpenFileDialog remember it has been in %temp%?如何让 OpenFileDialog 记住它已经在 %temp% 中?
【发布时间】:2016-08-12 09:47:22
【问题描述】:

我正在 Win8 桌面上的 C#.NET/Forms 中制作日志查看器应用程序。

我在使用 System.Windows.Forms.OpenFileDialog 时遇到问题。如果我从 %temp% 目录中选择一个文件并单击 OK,然后想要选择另一个文件,OpenFileDialog 拒绝记住我上次访问 %temp% 目录。它总是恢复显示我上次访问的非 %temp% 目录,这很烦人,因为我的应用程序通常会从 %temp% 打开各种日志文件。

Precondition: - OpenFileDialog created and existing. - OpenFileDialog has InitialDirectory = "C:\" Scenario: - ShowDialog(): Displays C:\ - OK. - Change directory to C:\logfiles\test.txt and click OK to close. - ShowDialog(): Displays C:\logfiles\ - OK. - Change directory to %temp% (which expands to C:\Users\joe\AppData\Local\Temp) and click OK to close. - ShowDialog(): Displays C:\logfiles\ - FAIL! Here it should show C:\Users\joe\AppData\Local\Temp but it doesn't. It reverts to the last directory I selected that is not in %temp%. Why?

问题:如何防止这种行为?

【问题讨论】:

  • 请查看以下链接。 stackoverflow.com/questions/17121296/…
  • 您可以使用FileDialog.RestoreDirectory 属性来记住最后一个目录。您也可以使用IntiialDirectory 打开它,但这两者可能会发生冲突-您也可以始终设置一个变量来记住最后一个目录:) 我不知道您的变量实例被称为什么,因为您实际上没有发布任何代码,但例如: var ofd = new OpenFileDialog(); ofd.RestoreDirectory = true;`
  • 已被virious回复here

标签: c# winforms


【解决方案1】:

您可以使用 InitialDirectory 属性记录:http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.initialdirectory.aspx

例子:

fdlg.InitialDirectory = Path.GetTempPath();

请查看以下链接。

OpenFileDialog.RestoreDirectory fails for %temp% location? Bug or Feature

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-07
    • 2013-10-08
    • 1970-01-01
    • 2022-12-03
    • 2011-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多