============================== UPDATE ==============================
I've tried using what Barrnet Chou suggested. I've created SelectDialog.h
and SelectDialog.cpp
files with the implementation mentioned in the comment, I've included SelectDialog.h
in the main .cpp
file and I've tried using the code from my main .cpp
file as follows:
CSelectDialog ofd(TRUE, _T("*.*"), NULL, OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT,
_T("All files and folders(*.*)|*.*||"));
CString path, nFile, info;
if (ofd.DoModal() != IDOK)
return;
for (int i = 0; i < ofd.m_SelectedItemList.GetCount(); i++) {
nFile = ofd.GetFileName();
path = ofd.GetFolderPath();
info = ofd.m_SelectedItemList[i].GetString();
}
MessageBox(path);
MessageBox(nFile);
MessageBox(info);
I don't know what did I do wrong, but I'm still having the same problem, I can only choose files and not folders. I've also tried adding the flag FOS_PICKFOLDERS
as IInspectable mentioned when I call the constructor of ofd
varibale, but it didn't help either. any ideas about what I've missed?
============================== ==============================
I'm writing a dialog based MFC application on Visual Studio 2017 in C++. I want to enable the user to choose a file or folder into which he will write later. After the user will choose the desired file or folder I want to save the path into pFile
variable for later use.
I've seen some questions on this subject, but all the answers to those posts provide only one functionality - either to choose a file or a folder.
what I've tried:
Method #1
OPENFILENAME file;
TCHAR szFileName[MAX_PATH] = _T("");
SecureZeroMemory(&file, sizeof(file));
file.lStructSize = sizeof(OPENFILENAME);
file.hwndOwner = NULL;
file.lpstrFilter = NULL;
file.lpstrFile = szFileName;
file.nMaxFile = MAX_PATH;
file.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
file.lpstrDefExt = "txt";
GetSaveFileName(&file);
pFile = file.lpstrFile;
Problem: Allows to choose only files as seen in the picture below:
Method #2
CFileDialog dlgFile(TRUE);
CString fileName;
const int c_cMaxFiles = 100;
const int c_cbBuffSize = (c_cMaxFiles * (MAX_PATH + 1)) + 1;
dlgFile.GetOFN().lpstrFile = fileName.GetBuffer(c_cbBuffSize);
dlgFile.GetOFN().nMaxFile = c_cbBuffSize;
CString file_name, dir_path;
if (dlgFile.DoModal() == IDOK) {
file_name = dlgFile.GetFileName();
dir_path = dlgFile.GetFolderPath();
pFile = dir_path + "\\" + file_name;
}
fileName.ReleaseBuffer();
Problem: Allows user to select files only as seen at the picture attached to Method #1
Method #3
LPCSTR m_strFolderPath = ("C:\\"); // Just for sample
LPCSTR m_strDisplayName;
CFolderDialog dlg(_T("Dialog Title"), m_strFolderPath, this);
if (dlg.DoModal() == IDOK)
{
pFile = dlg.GetFolderPath();
}
Problem: Allows the user to choose folders only as seen in the picture below:
Method #4
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(bi));
TCHAR szDisplayName[MAX_PATH];
szDisplayName[0] = (TCHAR)("");
bi.hwndOwner = NULL;
bi.pidlRoot = NULL;
bi.pszDisplayName = szDisplayName;
bi.lpszTitle = _T("Please select a folder for storing received files :");
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lParam = NULL;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
TCHAR szPathName[MAX_PATH];
if (NULL != pidl)
{
BOOL bRet = SHGetPathFromIDList(pidl, szPathName);
if (FALSE == bRet)
return;
AfxMessageBox(szPathName);
}
Problem: Allows user to select folders only as seen at the picture attached to Method #3
Method #5
I've tried using IFileDialog
for this purpose, but I'm not sure how to use it to choose a file or a folder. I think that's the solution I need, and I'll appreciate it if anyone could refer me to some examples for how to use it? I've seen some such as this from Microsoft Docs documentation and this one from git and also this and that one from stack overflow questions but I wasn't able to apply them. I've tried constructing something similar by using the previously mentioned codes, but I only got this far:
IFileDialog *pfd;
IFileSaveDialog *pfsd;
HRESULT hr = pfd->QueryInterface(&pfsd);
LPWSTR *pszName;
if (SUCCEEDED(hr)) {
hr = pfd->GetFileName(pszName);
if (SUCCEEDED(hr)) {
MessageBox((LPCSTR)pszName);
}
}
I'd really appreciate If anyone could explain me how to use IFileDialog to enable the user to choose files and folders, or if there's any other solution for this problem, I'd like to try it.
Thank you.