2016-08-12 14 views
0

我在Win8桌面上的C#.NET/Forms中創建日誌查看器應用程序。如何讓OpenFileDialog記住它已經在%temp%?

使用System.Windows.Forms.OpenFileDialog時出現問題。如果我從%temp%目錄中選擇一個文件並單擊確定,然後又想選擇另一個文件,則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?

問:如何防止這種行爲?

+0

請檢查以下鏈接。 http://stackoverflow.com/questions/17121296/openfiledialog-restoredirectory-fails-for-temp-location-bug-or-feature – zahed

+1

您可以使用'FileDialog.RestoreDirectory'屬性來記住最後一個目錄。你也可以使用IntiialDirectory打開它,但是這兩者可能會發生衝突 - 你總是可以設置一個變量來記住最後一個目錄:)我不知道你的變量實例被調用了,因爲你實際上沒有發佈任何東西代碼,但例如:var ofd = new OpenFileDialog(); ofd.RestoreDirectory = true;' –

+1

已通過virious回答[此處](http://stackoverflow.com/a/16078701/2503977)。 – Toastgeraet

回答

相關問題