last directory opened

IssacJones
IssacJones used Ask the Experts™
on
Hiya

I have built an SDI application using Visual C++ MFC. When I open a folder and save a file, close the application, open the application, the next time I open to load a file the application knows what the last directory was.

I suspect this information must be being stored in the registry.

Can somebody tell me how to retrieve this information?

Thanks

John
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
MFC applications use the registry to store this data -  in InitInstance method you can find these two lines:
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(4); // Load standard INI file options (including MRU);
If you will set a break point in this method, then with F11 you will see yourself what's going on and how it works.
In the registry you can find:
HKEY_CURRENT_USER\software\My Company\My Product.
http://www.codeproject.com/KB/docview/most_recent_used.aspx
 

Author

Commented:
ah, unfortunately I'm not using the registry explicitly in my application i.e. I'm using an INI file. Any other ideas? It looks as if the last folder opened is being save some other way.

Commented:
Save the same in the ini-file: GetPrivateProfileString, WritePrivateProfileString, etc:
http://msdn.microsoft.com/en-us/library/ms724353(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms725501(VS.85).aspx
The last link contains an example.
More:
http://www.codeproject.com/KB/cpp/IniReader.aspx
 
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Author

Commented:
Hi again

I think you may have misunderstood. I'm not trying to write the information to an ini file. What I'm trying to do is work out how my application knows what the last folder opened was during the last run. I haven't written any code to do it so the application must be using some implicit method to do it. What I want to know is how.

John

Commented:
I've answered in the first comment. You said it is MFC SDI application. Comment 26444408 is the answer.
 

Author

Commented:
hiya pgnatyuk

Err, but it doesn't explicitly use the registry i.e. SetRegistryKey(_T("Local AppWizard-Generated Applications")); is commented out in the application.

It is possible I suppose that you mean it is happening in

LoadStdProfileSettings(4); // Load standard INI file options (including MRU);

I will try looking at that and see if it is responsible.

John

Author

Commented:
No. Even commenting out

LoadStdProfileSettings(4); // Load standard INI file options (including MRU);

still allows the application to remember the last folder it opened up a file in.

Strange!

John
Commented:
if you press the symbol for Open a File, CWinApp calls the Function OpenDocumentFile(). This function calls the DoPromptFileName() function of the CDocManager() class. The DoPromptFileName()-function use an OFN-structure. In OFN-Structur is a InitialDir-String for Dialog initialization. Following is a snippet of MSDN-Documentation for the member InitialDir. The Result: I think what you want to know, is extracted by system from the MRU-List of your Application or the current working Directory. You must change the lpInitialDir-member on DoPromptFileName.

my CDocManager-class HeaderFile is saved in:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\atlmfc\src\mfc\docmgr.cpp


---------------------------------------------------------
lpstrInitialDir
Pointer to a NULL terminated string that can specify the initial directory. The algorithm for selecting the initial directory varies on different platforms.
---------------------------------------------------------
Windows 2000/XP:
If lpstrFile contains a path, that path is the initial directory.
Otherwise, lpstrInitialDir specifies the initial directory.
Otherwise, if the application has used an Open or Save As dialog box in the past, the path most recently used is selected as the initial directory. However, if an application is not run for a long time, its saved selected path is discarded.
If lpstrInitialDir is NULL and the current directory contains any files of the specified filter types, the initial directory is the current directory.
Otherwise, the initial directory is the personal files directory of the current user.
Otherwise, the initial directory is the Desktop folder.
---------------------------------------------------------
Windows 98/Me:
lpstrInitialDir specifies the initial directory.
If lpstrInitialDir is NULL and lpstrFile contains a path, that path is the initial directory.
Otherwise, if the current directory contains any files of the specified filter types, the initial directory is the current directory.
Otherwise, the initial directory is the personal files directory of the current user.
Earlier versions of Windows and Windows NT:
---------------------------------------------------------
lpstrInitialDir specifies the initial directory.
If lpstrInitialDir is NULL and lpstrFile contains a path, that path is the initial directory.
Otherwise, the initial directory is the current directory.



i hope this solve your Question

Author

Commented:
Hi again

Nobody seems to know where the last directory opened is being stored or maybe I have not explained myself properly. As such, here is a list of steps to re-create the scenario:

(1) Create a new SDI application using Visual Studio but using the old MFC settings i.e. don't use ribbons etc.
(2) Comment out the following two lines:

      //SetRegistryKey(_T("Local AppWizard-Generated Applications"));
      //LoadStdProfileSettings(4);  // Load standard INI file options (including MRU)

This will mean, if I undertand correctly, that the registry and INI files won't be used.

(3) Start the application and use File-Open to view a directory of your chosing (not the directory it shows at startup) and select a file (this can be anything because there is no code to load it).

(4) Close the application.

(5) Start the application again and choose File-Open.

(6) The directory that you last viewed will be displayed.

What I'm trying to find out is where is this information being stored? There is no INI file and the registry call has been commented out.

There must be someone who knows?

Hope you can help???

John
Commented:
I'm assuming that your program is going to use CFileDialog to open the save dialog.

Why can't you look at the code where CFileDialog is being called and see what variable is being set into fileDlg.m_ofn.lpstrInitialDir

Then if you can at least find that value you should be able to reverse engineer your program a bit and find out where the variable is being initialized from.

Author

Commented:
Hi

I've stepped into CFileDialog to see if I could see where

dlgFile.m_ofn.lpstrInitialDir

is being set but it doesn't seem to be doing so.

Any other ideas?

John

Author

Commented:
p.s. worse still, when I try to check the value

            dlgFile.m_ofn.lpstrInitialDir      CXX0017: Error: symbol "m_ofn" not found      

I don't quite understand why m_ofn cannot be accessed within the debugger.

John
Hi Issac,

Even though it is too late to discuss on this issue, since I have the info, I would like to share it with you and all here at EE.

Even though you have commented out those lines that could lead for your app to interact with the registry, since that you are launching the open file dialog (Windows Common Dialog), it---itself interacts with the registry. I meant, MS has designed it in such a way that it keeps storing the MRU paths at the following reg-address (excerpted is on XP box).
 
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU\*

And also in the subfolder of OpenSaveMRU whose name corresponds to the extension of the file that you had opened previously. i.e, if you had opened .txt, you will also get an entry under

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU\txt

Hope this clarifies your doubt. Or if you still have, you can ask me. But if you are satisfied, don't forget to close this question.

Author

Commented:
Hi Habeeballah

Excellent!!! That's exactly what I was looking for. As a matter of interest how did you know the information?

Many thanks for everybody elses help.

John
John,

To be frank, when I went through your question, suddenly the following tip flashed in my mind that I had read few years back.

http://www.codeproject.com/KB/files/tipoftheday.aspx 

So then it was easy to peek into the next available subkey OpenSaveMRU of the ComDlg32.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial