Solved

How to retrieve the most recently used directory

Posted on 2004-08-04
11
397 Views
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.
0
Comment
Question by:rschaaf
11 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 250 total points
Comment Utility
OpenFileDialog stores the last used folder in the registry.
0
 

Author Comment

by:rschaaf
Comment Utility
Where in the registry?
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
im not sure but I am quite sure you shouldnt be doing that :)
0
 
LVL 4

Expert Comment

by:NTAC
Comment Utility
You can save it in a file, and read that file each time you start--like a *.ini for your brogram.
0
 

Author Comment

by:rschaaf
Comment Utility
What I want is access to THAT path, the one used by the OpenFileDialog().  There must be some way to do it...
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
he is suggesting that you duplicate the logic ... when they save, save the folder they saved on your own in the registry.
0
 

Author Comment

by:rschaaf
Comment Utility
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.
0
 

Author Comment

by:rschaaf
Comment Utility
I think I found it.

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU\*

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

Author Comment

by:rschaaf
Comment Utility
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().
0
 
LVL 9

Expert Comment

by:s_sansanwal
Comment Utility
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

Cheers,
S Sansanwal
0
 

Author Comment

by:rschaaf
Comment Utility
I'm going to award the points to greoryyoung who pointed me to the registry.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now