【发布时间】: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?
问题:如何防止这种行为?
【问题讨论】:
-
您可以使用
FileDialog.RestoreDirectory属性来记住最后一个目录。您也可以使用IntiialDirectory打开它,但这两者可能会发生冲突-您也可以始终设置一个变量来记住最后一个目录:) 我不知道您的变量实例被称为什么,因为您实际上没有发布任何代码,但例如: var ofd = new OpenFileDialog(); ofd.RestoreDirectory = true;` -
已被virious回复here。