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

本文关键字:%temp% 何使 OpenFileDialog | 更新日期: 2023-09-27 18:02:19

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

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

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

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

的例子:

fdlg.InitialDirectory = Path.GetTempPath();

请查看下面的链接。

OpenFileDialog。%temp%位置的恢复目录失败?