How to retrieve the most recently used directory

Posted on 2004-08-04
Last Modified: 2012-05-05
When I use OpenFileDialog, and do not set the InitialDirectory, the dialog defaults to the directory previously selected by the user.  This works even when the app is closed and re-started.

How can I retrieve that most recently opened directory, so I can use it in a FolderBrowserDialog()?

I looked through Environment and Application namespaces, but every path I see points to a fixed directory somewhere, not to the most recent directory selected by the user.
Question by:rschaaf
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 37

Accepted Solution

gregoryyoung earned 250 total points
ID: 11719872
OpenFileDialog stores the last used folder in the registry.

Author Comment

ID: 11719979
Where in the registry?
LVL 37

Expert Comment

ID: 11720051
im not sure but I am quite sure you shouldnt be doing that :)
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Expert Comment

ID: 11720266
You can save it in a file, and read that file each time you start--like a *.ini for your brogram.

Author Comment

ID: 11721848
What I want is access to THAT path, the one used by the OpenFileDialog().  There must be some way to do it...
LVL 37

Expert Comment

ID: 11721856
he is suggesting that you duplicate the logic ... when they save, save the folder they saved on your own in the registry.

Author Comment

ID: 11723114
Okay - let me state my problem again.

I do not want to use OpenFileDialog.  I want to use FolderBrowserDialog() because I need the user to pick a directory, not a file.  But I want FolderBrowserDialog to default the the previously used directory, just like OpenFileDialog does when you set OpenFileDialog.InitialDirectory = null.

How can I discover what initial directory OpenFileDialog defaults to, without actually calling it?

I am increasing the points to 250.

Author Comment

ID: 11723175
I think I found it.


Look at MRUList key.  Take the first character. Look at the key with that letter.

Does anyone have a better way? I'm not sure that the '*' key name in the registry means...

Author Comment

ID: 11723249
This seems to be working, at least under XP/2000

RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU\*");
string MRUList = rk.GetValue("MRUList").ToString();
string MostRecent = MRUList[0].ToString();
string MRU = rk.GetValue(MostRecent).ToString();
string MRUPath = Path.GetDirectoryName(MRU);

MRUPath now contains the folder most recently used in the OpenFileDialog().

Expert Comment

ID: 11723265
Its not working on my computer.
Microsoft don't save this information in the registry and that why FolderBrowserDialog loses selectedpath after application is closed. FolderBrowserDialog selects same value until the object is not destroyed i.e it stores value on the stack.
To implement your functionality, you need to save selectedpath to registry yourself after ShowDialog(); and read it from registry at startup of application. Assign this value to selectedpath property of FolderBrowserDialog

S Sansanwal

Author Comment

ID: 11727419
I'm going to award the points to greoryyoung who pointed me to the registry.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question