I have an MFC C++ application, which has a CFileDialog. I call its DoModal function to open a file browse window. I set the lpstrInitialDir, to tell it where to open the dialog at the first time
CString defaultDir = L"C:\\tmp\\";
CFileDialog d(TRUE);
d.m_ofn.lpstrInitialDir = defaultDir ;
if( d.DoModal ()==IDOK )
{... app logic after the file was seslected...}
The problem is I would like my program to remember the user selection. Next time the user runs my app, I would like my DoModal filebrowse dialog to open at the location, from which the user selected the file at his last use.
How can I do it?
I saw there is LastVisitedMRU Registry key, nut I couldn't find any example how to properly use it with the CFileDialog.DoModal
Thanks a lot!
You won’t need to use “LastVisitedMRU” to accomplish this. Simply use the CWinApp::GetProfileString and CWinApp::WriteProfileString methods to read and write the path of the last accessed file. For example…
CString defaultDir = AfxGetApp()->GetProfileString(_T(“<registry key>"), _T("LastPath"));
CFileDialog d(TRUE);
d.m_ofn.lpstrInitialDir = defaultDir;
CString selectedPath = _T("");
BOOL rc = FALSE;
if (d.DoModal() == IDOK)
{
selectedPath = d.GetPathName();
rc = AfxGetApp()->WriteProfileString(_T("<registry key>"), _T("LastPath"), selectedPath);
}
Where, "registry key" is the value you used in the SetRegistry key call in your application's InitInstance method (if it’s not there, add it). And, "LastPath" is whatever you want for the registry sub-key.
NOTE: The sample code is from an MBCS project.