1

The default Microsoft Office 2010 behavior for persisting the file path of the Open File dialog seems to return to the last place it had been navigated.

  • Open Microsoft Word 2010.
  • Press Ctrl+O.
  • The OpenFileDialog opens to, say, C:\Temp\.
  • Navigate through the file tree to C:\Users\mike\Documents\. Don't select a file, and cancel the operation.
  • Press Ctrl+O again.
  • The dialog will open to the C:\Users\mike\Documents\ directory.

I'd like to duplicate this behavior in my WPF application. However the Microsoft.Win32.OpenFileDialog class doesn't seem to have a way to do this. The dialog returns false if Cancel was pressed (as advertised), but the FileName property doesn't get set if this is the case, and it appears to be impossible to capture the directory.

Any suggestions for how I can do this?

James Cronen
  • 5,715
  • 2
  • 32
  • 52
  • Create a static variable and store the path in it then you can check if the variable is null when showing the filedialog and then pass the variable to it –  Sep 06 '13 at 19:26
  • @PreciousTijesunimi: But I don't know the path the user will choose before the OpenFileDialog is opened. – James Cronen Sep 06 '13 at 19:29
  • I thought what you want to do is take the path the user entered when the user last opened the `openfiledialog` and clicked `cancel` and then show that same pathe when the `openfiledialog` is opened again???? –  Sep 06 '13 at 19:36
  • @PreciousTijesunimi: Yes, but the problem is capturing the path between when the user selects it and presses 'Cancel'. It could be anything and it doesn't seem to be residing in any of the `OpenFileDialog`'s properties. – James Cronen Sep 06 '13 at 19:39
  • The OpenFileDialog in WPF is virtually the same one than in Winforms (link -> http://stackoverflow.com/questions/12315908/difference-between-open-savefiledialog-classes-and-their-use-in-a-wpf-form) and thus should behave identically. I have checked it and cannot replicate the functionality you are referring in your question in winforms either. If I use OpenFileDialog (Winforms or WPF) and select a file in a folder; the next time it will start from this folder by default. If I go through folders but finally press cancel, no information would be stored. – varocarbas Sep 06 '13 at 19:48
  • @varocarbas: Thanks, I agree. But is there a way to store the current folder before 'Cancel' is pressed? – James Cronen Sep 06 '13 at 19:51
  • @Tenner I see the problem now –  Sep 06 '13 at 19:51
  • I haven't ever heard about such a thing and the properties of OpenFileDialog do not seem to indicate that might be such an option. What I usually do is redefining the InitialDirectory on account of the user selections. But without the user selecting something, I don't know. – varocarbas Sep 06 '13 at 19:54
  • @Tenner The only way i see out is creating your own openFileDialog –  Sep 06 '13 at 19:57
  • Ok, thanks. I suppose that's the only answer; I figured that Microsoft might have been nice enough to help out the rest of us by providing access to the same file dialog they used. – James Cronen Sep 06 '13 at 20:00
  • 1
    See http://stackoverflow.com/q/4106120/56778. Some promising leads there. Word *does* use the same base dialog control. They just customize it. – Jim Mischel Sep 06 '13 at 20:01
  • You'll have to get to the IFileDialogEvents. It isn't entirely impossible but you'll get no help from WPF. – Hans Passant Sep 06 '13 at 20:15

1 Answers1

1

Thanks to everyone who responded. The answer appears to be that there is no answer.

Thanks to Jim Mischel for this link in the comments, which may provide a start: stackoverflow.com/q/4106120/56778

Also to Hans Passant for his comment 'You'll have to get to the IFileDialogEvents. It isn't entirely impossible but you'll get no help from WPF.'

I have a deadline for which this feature isn't critical, so I'm abandoning this for now. If I ever come up with a solution I'll post it here.

Community
  • 1
  • 1
James Cronen
  • 5,715
  • 2
  • 32
  • 52