Show Files in CFolderPickerDialog C++ - c++

When utilizing CFolderPickerDialog in a MFC application, is there any way I can view the files as well. I understand that there are way cool options to select a folder, but I need to make sure the files I want are in that directory as well. Any help would be greatly appreciated.

The CFolderPickerdialog not allowing users to see the files within the dialog box came as a WYSIWYG moment. SO your option then becomes to utilize the CFileDialog and then trim the path to Folderpath as described in the code below.
CFileDialog dlg (OFNHIDERADONLY | OFN_OVERWRITEPROMPT | BIF_BROWSEINCLUDEFILES, NULL);
...
CString path = dlg.GetPathName();
CString Folderpath = path.Left(path.ReverseFind('\\'));
if (Folderpath.Right(1) != "\\")
Folderpath += "\\";
Therefore, your code may look like C:\Users\foldername\file.txt
but will now look like this C:\Users\foldername\

Related

CFileDialog with realtive path

I currently maintenance an old MFC application and have problems with opening file dialogs. The Program has multiple different parts were the user select files for loading, eg sound, video and other program specific formats.
Opening a dialog should always open in a "specific" folder, depending on the file ending. Giving an directory path that contains "..\" will accept and the Dialog opens with the "last selected file".
CString fileDirectory = myHelper.getPath();
// fileDirectory is now "C:\coding\svn\source\MyProgram\..\..\bin\..\data\..\Audio\"
CFileDialog FileDialog(true, _T("MP3;WAV"), _T(fileDirectory), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, _T("All music files (*.WAV;*.MP3)));
if (FileDialog.DoModal() == IDOK)
{ ... }
I use different CDialog classes (about 15, eg for editing audiofiles, for videofiles) and they all have similar code for opening dialogs like above.
How can i support the relative paths for the CFileDialog?
The CFileDialog supports setting up initial/default folder. Here is the code snippet that demonstrates how to use it:
const TCHAR szFilter[] = _T("Parameter Files (*.npf)|*.npf|All Files (*.*)|*.*||");
CFileDialog dlg(TRUE, _T("npf"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
CString sParametersDir(CUtility::GetParametersDir());
dlg.m_ofn.lpstrInitialDir = sParametersDir.GetBuffer(_MAX_PATH);
if(dlg.DoModal() == IDOK)
{
m_ParametersFileEdit.SetWindowText(dlg.GetPathName());
}
sParametersDir.ReleaseBuffer();
Also regarding your code. There is no need to use _T() macro for CString objects. The CString class does support UNICODE automatically. The _T() macro should only be used for string literals.
You can use CPath class to normalize file path.
CPath path(sPath);
path.AddBackslash();
path.Append(_T("Config"));
path.Canonicalize();

QFileDialog showing hidden files although system setting is off

I am using the following code to show an open dialog in Qt:
QString path = QFileDialog::getOpenFileName(this, tr("Open Config File"), QDir::rootPath(), "Text Files (*.txt *.csv *.*);;");
What I realised is that this dialog also shows hidden files although the system setting for showing hidden files is turned off. It's the same if I instantiate the QFileDialog manually and show it. I also couldn't find out how to turn this off via a filter.
Does anyone know if there is a way to achieve the desired behaviour?
Looks like there is no simple(by setting some flag) solution out there. So I recommend to use the filtering which is described in other SO answer.
But in your case you might use the following condition:
if(fileModel != nullptr)
{
QFileInfo info = fileModel->fileInfo(index0);
return info.isHidden();
}
return false;

How to select specific file using CfileDialog in MFC

I would like to open a file dialog and allow user to select only the file with name "myapplication.ini" and user can only browse folder's to check if the file is existing to select it.
so i came across CFileDialog which would do almost what i want other than limiting it to display only files with name "myapplication.ini"
currently my usage of CFiledialog
CFileDialog FileDialog(TRUE,"features.ini", NULL,OFN_HIDEREADONLY,NULL);
I am not sure what could should be changed to make it work as i expected.
That sounds like a poor UI. Even if you filter out all but that file, the user can override the filter. If you aren't going to allow the user to make a choice of file name, why ask them for their choice?
What you are actually doing, in my view, is asking the user to select a folder. So instead of the file dialog, show them a folder selection dialog, CFolderPickerDialog.
Declare the filter string like this:
static TCHAR BASED_CODE szFilter[] = _T("features.ini (features.ini)|features.ini|");
and then pass it to your CFileDialog ctor:
CFileDialog FileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);

Issues with CFileDialog for multiple file selection

I'm using the following code to retrieve multiple file selection via UI:
CFileDialog fd(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_EXPLORER,
NULL, hParentWnd ? CWnd::FromHandle(hParentWnd) : NULL);
fd.m_pOFN->Flags |= OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_NODEREFERENCELINKS;
int nLnBuff = 32767;
TCHAR* pBuffFileSelect = new TCHAR[nLnBuff];
memset(pBuffFileSelect, 0, nLnBuff * sizeof(TCHAR));
fd.m_ofn.lpstrFile = pBuffFileSelect;
fd.m_ofn.nMaxFile = nLnBuff;
if(fd.DoModal() == IDOK)
{
POSITION fileNamesPosition = fd.GetStartPosition();
while(fileNamesPosition != NULL)
{
CString strSelPath = fd.GetNextPathName(fileNamesPosition);
TRACE("path: %s\n", CStringA(strSelPath));
}
}
delete[] pBuffFileSelect;
So when I try it on my PC, I run the method above and when the "Open File" dialog open up, just as a test, I navigated to my desktop and selected all files using Ctrl+A shortcut and then clicked Open. As a result I started getting the following paths:
The first path is a link, which is correct (it exists on my Public desktop):
"C:\Users\Public\Desktop\avp.lnk"
But then the second path is wrong. It gives me:
"C:\Users\Public\Desktop\1.txt"
when it's supposed to be (for the desktop that I picked):
"C:\Users\UserName\Desktop\1.txt"
and then every consecutive path has "Public" instead of "UserName".
I should point out that I have several user accounts set up on this PC and the one that I'm testing this method from is a Standard user account. The app that I'm running this method from is not running elevated (or with regular user privileges) so it should not have access to other user accounts anyway.
So what am I doing wrong here?
Checked the sources, and GetOpenFileName assumes that all the items are in fact in the same file path. This isn't true for the Desktop (there are items in different paths merged into one shell view), and so you'll see the bad behavior.
The solution is to use the Common Item dialogs, which use the shell namespace rather than file system paths. All the desktop items are in a common shell path, and then you can use IShellItem::GetDisplayName to convert to a file system path.
Unfortunately, MFC doesn't have a wrapper for the common item dialog, so you'll have to manage that yourself.

Allowing IFileOpenDialog to pick a folder that doesn't exist yet

I'm trying to create a dialog to select a new folder to save files into. The IFileOpenDialog class works great except that it won't allow a new folder to be picked.
I.e. "Folder: C:\existings\new-folder" in the bottom of the dialog pops up the following message:
new-folder
Path does not exist.
Check the path and try again.
Here's the code I've got:
IFileDialog* fileDialog
CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&fileDialog));
DWORD options;
fileDialog->GetOptions(&options);
options &= ~FOS_FILEMUSTEXIST;
options &= ~FOS_PATHMUSTEXIST;
fileDialog->SetOptions(options | FOS_PICKFOLDERS);
fileDialog->Show(parentWindow);
Any pointers or hacks would be appreciated! Thanks!
To quote Michael from this other question:
[To head off some comments, the SHBrowseForFolder API still exists, but is still not an acceptable solution for our UI deciders.]
The same applies here...
I think you want to use CLSID_FileSaveDialog instead of CLSID_FileOpenDialog. And possibly make use of IFileSaveDialog in addition to the base class IFileDialog.
As you're selecting a folder, you could use the folder picker dialog.
This, with the right flags, has a "create" button at the bottom and a text entry allowing you to specifiy a non existant path.