0

I'm making a log viewer application in C#.NET/Forms on Win8 desktop.

I have a problem when using the System.Windows.Forms.OpenFileDialog. If I select a file from the %temp% directory and click OK, and then want to select another file, the OpenFileDialog refuse to remember that I last visited the %temp% directory. It always reverts to displaying the last non-%temp% directory I visited, which is very annoying since my app typically opens various log files from %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?

Question: How to prevent this behavior?

  • Please check the below link. http://stackoverflow.com/questions/17121296/openfiledialog-restoredirectory-fails-for-temp-location-bug-or-feature – Mr doubt Aug 12 '16 at 09:51
  • 1
    You can use the `FileDialog.RestoreDirectory` property to remember the last directory. You could alternatively open it using `IntiialDirectory`, but these two might clash - you could always set a variable to remember the last directory, too :) I don't know what your variable instance is called as you haven't actually posted any code, but for example: var ofd = new OpenFileDialog(); ofd.RestoreDirectory = true;` – Geoff James Aug 12 '16 at 09:51
  • 1
    Has already been answered [here](http://stackoverflow.com/a/16078701/2503977) by virious. – Toastgeraet Aug 12 '16 at 09:52
  • Possible duplicate of [How to save last folder in openFileDialog?](http://stackoverflow.com/questions/16078362/how-to-save-last-folder-in-openfiledialog) – Geoff James Aug 12 '16 at 10:00

1 Answers1

0

You can use InitialDirectory property documented: http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.initialdirectory.aspx

Example:

fdlg.InitialDirectory = Path.GetTempPath();

Please check the below link.

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

Community
  • 1
  • 1
Mr doubt
  • 51
  • 1
  • 10
  • 42