I use the MFC
interface IFileOpenDialog
for selecting the source data file. Obviously, dialog is open in last used directory (folder).
I want to open the dialog in folder, the user send to function (the path in the parameter lsCurFolder
). I have not find how to do it.
inline bool OpenFile(LPTSTR& pszFilePath, const FileType fTyp = MajCut, LPCTSTR lsCurFolder) {
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
IFileOpenDialog* pFileDlg = NULL;
// Create the FileOpenDialog object.
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileDlg));
if (!SetTyp(fTyp, pFileDlg, L"Vyhledej soubor pro načtení"))
{
pFileDlg->Release();
return false;
}
FILEOPENDIALOGOPTIONS opt;
pFileDlg->GetOptions(&opt);
if (pFileDlg->SetOptions(opt | FOS_STRICTFILETYPES) != S_OK)
{
pFileDlg->Release();
return false;
}
if (SUCCEEDED(hr))
{
// Show the Open dialog box.
hr = pFileDlg->Show(NULL);
// Get the file name from the dialog box.
if (SUCCEEDED(hr))
{
IShellItem* pItem;
hr = pFileDlg->GetResult(&pItem);
if (SUCCEEDED(hr))
{
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath); //Volající musí zrušit volání voláním CoTaskMemFree
pItem->Release();
}
}
pFileDlg->Release();
}
CoUninitialize();
}
return SUCCEEDED(hr);
}
Per 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 callingShow
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.
You can force the dialog to always show the same folder when it opens, regardless of previous user action, by calling
IFileDialog::SetFolder
. However, we do not recommended doing this. If you callSetFolder
before you display the dialog box, the most recent location that the user saved to or opened from is not shown. Unless there is a very specific reason for this behavior, it is not a good or expected user experience and should be avoided. In almost all instances,IFileDialog::SetDefaultFolder
is the better method.
So, for example:
...
IShellItem *pCurFolder = NULL;
hr = SHCreateItemFromParsingName(lsCurFolder, NULL, IID_PPV_ARGS(&pCurFolder));
if (SUCCEEDED(hr))
{
pFileDlg->SetFolder(pCurFolder);
pCurFolder->Release();
}
hr = pFileDlg->Show(NULL);
...