如何使OpenFileDialog记住它已经在%temp%?

时间:2016-08-12 09:47:23

标签: c# winforms

我在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?

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

1 个答案:

答案 0 :(得分:0)

您可以使用记录的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