我发现以下代码允许我浏览文件夹
CFileDialog od(TRUE/*bOpenFileDialog*/, NULL, NULL, OFN_HIDEREADONLY |
OFN_OVERWRITEPROMPT , NULL, NULL, 0, TRUE/*bVistaStyle*/);
IFileOpenDialog * openDlgPtr = od.GetIFileOpenDialog();
if ( openDlgPtr != NULL )
{
openDlgPtr->SetOptions(FOS_PICKFOLDERS);
openDlgPtr->Release();
}
int r = od.DoModal();
它打开一个文件对话框确定,我可以选择一个文件夹,打开按钮变为启用,但按下它只是打开文件夹,它不会选择它。除非我点击取消
,否则DoModal不会返回如何在MFC中选择文件夹的任何想法?感谢
顺便说一句,我知道CFolderDialog http://www.codeproject.com/Articles/2024/CFolderDialog-Selecting-Folders?msg=4497794#xx4497794xx
不错的项目,但是当我选择我的USB挂载的android文件夹时,对话框返回不正常,所以除非我能修复它否则对我没用#/ p>
更新
我也找到了这个
BROWSEINFO bi = { 0 };
TCHAR path[MAX_PATH];
bi.lpszTitle = _T("Pick a Directory");
bi.pszDisplayName = path;
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
// get the name of the folder
//_tprintf ( _T("Selected Item: %s\n"), path );
// free memory used
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
setMobilePath(path);
}
这允许我在我的Android设备上选择一个文件夹,但它不会返回完整路径,只是文件夹名称也没用多少
答案 0 :(得分:1)
将返回的pidl转换为字符串,如下所示:
BROWSEINFO bi = { 0 };
bi.lpszTitle = _T("Pick a Directory");
LPITEMIDLIST pidl = SHBrowseForFolder (&bi);
if (pidl != 0)
{ // convert pidl to string
TCHAR szPath[MAX_PATH];
SHGetPathFromIDList(pidl, szPath);
// free memory used
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc(&imalloc)))
{ imalloc->Free (pidl);
imalloc->Release();
}
//_tprintf(_T("Selected Item: %s\n"), szPath);
setMobilePath(szPath);
}
答案 1 :(得分:0)
尝试这个
CFolderPickerDialog dlgFolder;
if (dlgFolder.DoModal() == IDOK)
{
CString strFolder = dlgFolder.GetPathName();
AfxMessageBox(strFolder);
}