Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initial directory is not working for CFileDialog

I am using CFileDialog, I have set the initial path like below , as shown in the code. It's not working . Correct me if I made a mistake.

   CFileDialog* filedlg = new CFileDialog(TRUE,(LPCTSTR)NULL ,  (LPCTSTR)NULL , OFN_HIDEREADONLY| OFN_ENABLESIZING , (LPCTSTR)NULL , FromHandle (hImgDlg) ,0 , FALSE  );

   filedlg ->m_ofn.lpstrInitialDir = "C:\\" ;

   if ( filedlg ->DoModal() == IDOK )
   {
       /***  do somthing here *****/
   }
like image 258
jack Avatar asked Apr 23 '13 08:04

jack


3 Answers

If you see the reference for the OPENFILENAME structure, you will see that for the lpstrInitialDir field it states that:

If lpstrInitialDir has the same value as was passed the first time the application used an Open or Save As dialog box, the path most recently selected by the user is used as the initial directory.

This means that the lpstrInitialDir field can really only be used the first time you use the dialog in a program. The rest of the time it will use the last directory selected by the user.

like image 122
Some programmer dude Avatar answered Oct 21 '22 19:10

Some programmer dude


If you set the filename location, you can get the dialog to open to a specific location. I would only use this if you really needed the folder location to open or if you have a default filename that you use.

CFileDialog* filedlg = new CFileDialog(TRUE, (LPCTSTR)NULL,  (LPCTSTR)_T("C:\\MyFolder\\DefaultFileName.ext"), OFN_HIDEREADONLY | OFN_ENABLESIZING, (LPCTSTR)NULL, FromHandle (hImgDlg), 0, FALSE);

or you could use the Windows function GetModuleFileName:

CString csAppFolder;
TCHAR szPath[MAX_PATH]; 

// form the path to where we want to store the file
if (GetModuleFileName(NULL, szPath, MAX_PATH))
{
    PathRemoveFileSpec(szPath);
    csAppFolder = szPath;
}

CFileDialog* filedlg = new CFileDialog(TRUE, (LPCTSTR)NULL, (LPCTSTR)(csAppFolder + _T("\\DefaultFileName.ext")), OFN_HIDEREADONLY | OFN_ENABLESIZING, (LPCTSTR)NULL, FromHandle (hImgDlg), 0, FALSE);
like image 38
CaptainBli Avatar answered Oct 21 '22 20:10

CaptainBli


Two options: 1. Old-fashioned dialog style, specifying OFN::lpstrInitialDir

CFileLatinDialog dlg (TRUE, "", "" /*lpszFileName */,
   OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
   "All Files(*.*)|*.*||", this, 0,
   FALSE /*bVistaStyle*/);
dlg.m_ofn.lpstrInitialDir = "C:\\Models\\";
  1. Vista style dialog, specifying lpszFileName parameter
CFileLatinDialog dlg (TRUE, "", "C:\\Models\\" /*lpszFileName */,
   OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY,
   "All Files(*.*)|*.*||", this);
like image 27
blackbada_cpp Avatar answered Oct 21 '22 20:10

blackbada_cpp