一、问题代码
CString szPath;
BROWSEINFO bi;
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath.GetBuffer();
bi.lpszTitle = L " 请选择目录: " ;
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0 ;
bi.iImage = 0 ;
// 弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolder( & bi);
if (lp && SHGetPathFromIDList(lp, szPath.GetBuffer()))
AfxMessageBox(szPath);
else
AfxMessageBox(L " 无效的目录,请重新选择 " );
BROWSEINFO bi;
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath.GetBuffer();
bi.lpszTitle = L " 请选择目录: " ;
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0 ;
bi.iImage = 0 ;
// 弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolder( & bi);
if (lp && SHGetPathFromIDList(lp, szPath.GetBuffer()))
AfxMessageBox(szPath);
else
AfxMessageBox(L " 无效的目录,请重新选择 " );
二、报错截图
三、解决错误
TCHAR szPath[MAX_PATH];
BROWSEINFO bi;
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = L " 请选择目录: " ;
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0 ;
bi.iImage = 0 ;
// 弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolder( & bi);
if (lp && SHGetPathFromIDList(lp, szPath))
AfxMessageBox(CString(szPath));
else
AfxMessageBox(L " 无效的目录,请重新选择 " );
BROWSEINFO bi;
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = L " 请选择目录: " ;
bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0 ;
bi.iImage = 0 ;
// 弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolder( & bi);
if (lp && SHGetPathFromIDList(lp, szPath))
AfxMessageBox(CString(szPath));
else
AfxMessageBox(L " 无效的目录,请重新选择 " );
说明:把CString改成了TCHAR数组。
本文转自博客园农民伯伯的博客,原文链接:[MFC]设置文件夹目录时遇到的问题,如需转载请自行联系原博主。