.net windows api: get or set most recent directory used by file open dialog

StackOverflow https://stackoverflow.com/questions/4423121

  •  09-10-2019
  •  | 
  •  

سؤال

Is there a way to set or get the initial directory used by the vista file open dialog without setting the InitialDirectory property on the file open dialog?

I want my WPF to (on startup) set the default folder once for all the Open/Save dialogs. If you notice in Vista as long as the machine is running it continues to default/track the user's last selected directory from any file open/save as dialogs. Notepad or any programs act this way.

The reason I am trying to accomplish this is because I don't want to have to track it within my app, setting and saving the folder. I would just like to set the default and let vista track it after that.

Directory.SetCurrentDirectory does not do it.

هل كانت مفيدة؟

المحلول

You are excluding the exact thing you have to do to solve your problem. If you don't set InitialDirectory then the dialog uses the last used directory. This info is stored in the registry, indexed by process name. You cannot selectively turn this behavior off.

The registry key that's used has been changing between Windows version. I think on Win7 it is now HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedPidlMRU. It is no longer in cleartext like it used to be on earlier versions. If you want to risk trying to parse the key values listed there then do make sure you verify the Windows version. Frankly, this isn't worth the simple code you'd have to write to initialize the InitialDirectory property.

نصائح أخرى

If you don't want to have to write code to keep loading and saving the directory for OpenFileDialog, write a wrapper function which does this, then just call your wrapper function...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top