2

i found the following code to allow me to browse for a folder

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();

it opens a file dialog ok and i can select a folder and the Open button becomes enabled, but pressing it just opens the folder, it doesn't select it. DoModal doesn't return unless i hit Cancel

any ideas how i can select a folder in MFC? thanks

by the way, i know about CFolderDialog http://www.codeproject.com/Articles/2024/CFolderDialog-Selecting-Folders?msg=4497794#xx4497794xx

nice project but when i select my USB mounted android folder the dialog returns not OK so its no use to me unless i can fix it

UPDATE

i also found this

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);
}

which does allow me to select a folder on my android device but it doesn't return the full path, just the folder name which isn't much use either

steveh
  • 1,352
  • 2
  • 27
  • 41
  • 1
    i realized that what i am trying to do is fundamentally wrong anyhow. the android device only shows up on windows then the user selects USB debug mode on the android but most normal people don't do that, so its not a good way to sync my windows app with my android app – steveh Feb 18 '13 at 00:19

2 Answers2

1

Convert the returned pidl to string as follows:

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);
}
mfc
  • 565
  • 2
  • 6
  • thanks, i tried that but it crashes when i choose the andoird device folder. i guess that's a special mount point, i need to use something else at access it. – steveh Feb 18 '13 at 00:05
0

try this one

CFolderPickerDialog dlgFolder;

if (dlgFolder.DoModal() == IDOK)
{
    CString strFolder = dlgFolder.GetPathName();
    AfxMessageBox(strFolder);
}
Vic.S
  • 23
  • 6