Malior
Malior

Reputation: 1341

Find out "last directory" of SaveFileDialog

I have a view, that contains a text-field and a button. The button shows a SaveFileDialog - The selected file-path will be assigned to the text-field.

  var saveFileDialog = new System.Windows.Forms.SaveFileDialog();
  var lastPath = saveFileDialog.InitialDirectory; //empty

What I'd like to have, is to know the location, in which the SaveFileDialog is opening on ShowDialog.

I know, that I can set the InitialDirectory, but this property is empty by default. And I explicitely dont want to set the InitialDirectory, my goal is to obtain the one the form obviously remembers somehow.

Is there a way to get this (whitout extra "saving" it for the next call).

Upvotes: 0

Views: 552

Answers (1)

NoviceProgrammer
NoviceProgrammer

Reputation: 3365

The path comes from the registry -

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\LastVisitedPidlMRU

or for older Windows OS HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\OpenSaveMRU

Also note what @Jimi mentioned about the RestoreDirectory property.

Edit: I had initially thought that the regkey/value was just a unicode string but it is not so straight forward to use. So reconsider this approach. If you really need to figure out how the keys work I suggest you take a look at - https://github.com/aelij/svcperf/blob/master/src/Viewer/UIUtils/MruFileHelper.cs

Upvotes: 2

Related Questions