jack
jack

Reputation: 375

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 *****/
   }

Upvotes: 8

Views: 8917

Answers (4)

Jan Raufelder
Jan Raufelder

Reputation: 287

I am a bit late to the party but just for anyone else who faces isssues. In my application multiple file dialogs are used in various places and for different purposes. So you are likely to be in different folders as a user when saving or loading a file. Hence it was a very bad user experience to always get the last used folder presented on the startup of any Dialog. After a lot of time wasting I finally find the docu on msdn which states it clearly:

Controlling the Default Folder

Almost any folder in the Shell namespace can be used as the default folder for the dialog (the folder presented when the user chooses to open or save a file). Call IFileDialog::SetDefaultFolder prior to calling Show to do so. The default folder is the folder in which the dialog starts the first time a user opens it from your application. After that, the dialog will open in the last folder a user opened or the last folder they used to save an item. See State Persistence for more details."*

State Persistence

Prior to Windows Vista, a state, such as the last visited folder, was saved on a per-process basis. However, that information was used regardless of the particular action. For example, a video editing application would present the same folder in the Render As dialog as it would in the Import Media dialog. In Windows Vista you can be more specific through the use of GUIDs. To assign a GUID to the dialog, call iFileDialog::SetClientGuid.

And that was all I needed. SetDefaultFolder for the first time opening for a user + SetClientGuid so that each instance remembers their own location, for each other opening.

IFileOpenDialog* dlg = fileDialog->GetIFileOpenDialog(); // fileDialog == CFileDialog*
if (dlg) 
dlg->SetClientGuid({0x6666a009, 0x6251, 0x4132, 0x23, 0xa5, 0xd4, 0x6, 0x69, 0xc2, 0x89, 0xf}); 
//new GUID for each different Dialog

This means OP could try to create and assign a new GUID on each opening to make sure that the initial DIR is always used. This feels more like a workaround though, but I wanted to point out the connection of the GUID and the initial DIR with my answer. For my purpose, seperating each different Dialog by a single GUID was enough. If the user really navigates somewhere else for a certain action then they probably know what they are doing and will end up in their folder of choice again on the next opening for the same action(dialog).

Upvotes: 0

blackbada_cpp
blackbada_cpp

Reputation: 432

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);

Upvotes: 6

CaptainBli
CaptainBli

Reputation: 4201

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);

Upvotes: 5

Some programmer dude
Some programmer dude

Reputation: 409442

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.

Upvotes: 6

Related Questions