Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

CFileDialog & Create/Choose Folder

Can I use CFileDialog to allow users to choose or create a folder for file save? If not, what should I use to accomplish it? I only need to choose or create a folder and return its path in CString. Thanks!
0 Kudos
Message 1 of 7
(8,613 Views)
The Windows Shell API SHBrowseForFolder function will let you do this. For example, if you have an MFC application project with an MFC dialog class, add #include <atlbase.h> to your stdafx.h, then add this function to your project:

static bool ChooseFolder(HWND hParent, const CString& title, CString& folder)
{
bool success = false;

BROWSEINFO bi;
::ZeroMemory(&bi, sizeof(bi));
LPTSTR pBuffer = folder.GetBuffer(MAX_PATH);

bi.hwndOwner = hParent;
bi.pszDisplayName = pBuffer;
bi.lpszTitle = title;
bi.pidlRoot = 0;
bi.ulFlags = BIF_RETURNONLYFSDIRS |
BIF_NEWDIALOGSTYLE;

LPITEMIDLIST pItem = ::SHBrowseForFolder(&bi);
if (pItem != NULL)
{
::SHGetPathFromIDList(pItem, pBuffer);
success = true;

CComPtr<IMalloc> pMalloc;
if (SUCCEEDED(::SHGetMalloc(&pMalloc)))
pMalloc->Free(pItem);
}

folder.ReleaseBuffer();
return success;
}

Then you can use this function from your dialog class like this:

CString folder;
if (ChooseFolder(m_hWnd, _T("Choose a folder:"), folder))
MessageBox(folder);

- Elton
Message 2 of 7
(8,613 Views)
Thanks, Elton. I followed your direction and it complains "'BIF_NEWDIALOGSTYLE' : undeclared identifier" when I compiled. I tested it in a simple dialog-based project created by NI Measurement Studio AppWizard. (1) How can I remove that compilation error? (2) Also, what should I change if I want to "create" a folder? and (3) Can I have both options (choose and create) in the same dialog? Thanks again for your help.
0 Kudos
Message 3 of 7
(8,613 Views)
(1) How can I remove that compilation error?

According to the documentation for BROWSEINFO, the BIF_NEWDIALOGSTYLE flag depends on version 5.0 of the Windows shell. See Shell and Common Controls Versions on MSDN for more info on this version of the shell. I'm guessing that the compilation error is happening because you're using Visual C++ 6.0 and you haven't updated to a newer version of the Platform
SDK
. The headers that shipped with Visual C++ 6.0 would not have symbols for features in version 5.0 of the shell since it had not shipped at that time. Upgrading to a newer Platform SDK or Visual C++ .NET should resolve the compilation error.

(2) Also, what should I change if I want to "create" a folder?

The BIF_NEWDIALOGSTYLE flag enables, among other things, a "Make New Folder" button on the dialog that will allow you to create a folder in the dialog. See the documentation for BROWSEINFO for more information.

(3) Can I have both options (choose and create) in the same dialog?

Yes, with the BIF_NEWDIALOGSTYLE flag. Without that flag you can only choose.

- Elton
0 Kudos
Message 4 of 7
(8,613 Views)
Elton, I downloaded the updates and it works now. I still needed to change the order and the list of link directories in VC++, but it is working now. Thanks for your prompt help.
0 Kudos
Message 5 of 7
(8,613 Views)
Elton, when I run the code, the file directory always starts from the desktop, but would like to start from the current working directory. I called "getcwd" and passed the char array to the function "ChooseFolder" as follows:

In the main function:
char buffer[_MAX_PATH];
_getcwd( buffer, _MAX_PATH )

In the ChooseFolder function:
bi.pidlRoot = (LPCITEMIDLIST)buffer;

But, I get an error "This folder cannot be used." How can I achieve it? Thanks.
0 Kudos
Message 6 of 7
(8,613 Views)
Please see the response that was posted in the new thread about this topic. Thanks.

- Elton
0 Kudos
Message 7 of 7
(8,613 Views)