0

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).

Malior
  • 1,221
  • 8
  • 16
  • 3
    Possible duplicate of [Setting the initial directory of an SaveFileDialog?](https://stackoverflow.com/questions/1175242/setting-the-initial-directory-of-an-savefiledialog) – Avi Meltser May 16 '19 at 11:19
  • Have you set the [FileDialog.RestoreDirectory](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.filedialog.restoredirectory) property? It will restore the last directory for you (if this is what you mean with `without extra "saving" it for the next call.`). – Jimi May 16 '19 at 11:25
  • I dont want to set the initial directory. I want to get the path, that the dialog will show on opening, without influencing it. – Malior May 16 '19 at 11:40

1 Answers1

2

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

NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32
  • I assume this is what I was searching for. But what exatly is this format of paths? I've tried now different variation of extracting the paths out of that binaries, but without success. – Malior May 20 '19 at 06:38