locked
Multiple files selection using open dialog RRS feed

  • Question

  • When i done selecting the files, the variable 'szFileName' doesn't hold the filenames but only the path to directory containing those files. how would i get the filenames and add them to a list?


    //Open File Dialog
    
    OPENFILENAME ofn; //file info struct
    
    TCHAR     szFileName[MAX_PATH];
    szFileName[0] = '\0';
    ofn.lStructSize       = sizeof (OPENFILENAME) ;
    ofn.hwndOwner         = m_hwnd ;
    ofn.hInstance         = NULL ;
    ofn.lpstrFilter       = NULL ;
    ofn.lpstrCustomFilter = NULL ;
    ofn.nMaxCustFilter    = 0 ;
    ofn.nFilterIndex      = 0 ;
    ofn.nMaxFile          = MAX_PATH ;
    ofn.lpstrFileTitle    = NULL ;          // Set in Open and Close functions
    ofn.nMaxFileTitle     = MAX_PATH ;
    ofn.lpstrInitialDir   = NULL ;
    ofn.lpstrTitle        = "Choose File(s)" ;
    ofn.Flags             = OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT|OFN_EXPLORER ;             // Set in Open and Close functions
    ofn.nFileOffset       = 0 ;
    ofn.nFileExtension    = 0 ;
    ofn.lpstrDefExt       = NULL ;
    ofn.lCustData         = 0 ;
    ofn.lpfnHook          = NULL ;
    ofn.lpTemplateName    = NULL ;
    ofn.lpstrFile         = szFileName;
    
    if (GetOpenFileName (&ofn))
    {
    	//szFileName contains the full path to file
    	SendMessage(GetDlgItem(m_hwnd, IDC_EDIT), WM_SETTEXT, 0, (LPARAM)szFileName);
    }
    
    Wednesday, December 23, 2009 12:59 PM

Answers

  • It works correctly. By default, you cannot see file names in Value viewer for szFileName (only path before first '\0'). Click on (+) near szFileName value, scroll down and you will see them.
    • Proposed as answer by Nikita Leontiev Thursday, December 24, 2009 11:17 AM
    • Marked as answer by Wesley Yao Friday, December 25, 2009 4:22 AM
    Wednesday, December 23, 2009 10:32 PM

All replies

  • From msdn:

    If the OFN_ALLOWMULTISELECT flag is set and the user selects multiple files, the buffer contains the current directory followed by the file names of the selected files. For Explorer-style dialog boxes, the directory and file name strings are NULL separated, with an extra NULL character after the last file name. For old-style dialog boxes, the strings are space separated and the function uses short file names for file names with spaces.
    Wednesday, December 23, 2009 1:03 PM
  • yes, the buffer should contain the current directory followed by the file names of the selected files . but i get the path to the current directory only. could you explain how would i get the filenames?
    Wednesday, December 23, 2009 1:13 PM
  • It works correctly. By default, you cannot see file names in Value viewer for szFileName (only path before first '\0'). Click on (+) near szFileName value, scroll down and you will see them.
    • Proposed as answer by Nikita Leontiev Thursday, December 24, 2009 11:17 AM
    • Marked as answer by Wesley Yao Friday, December 25, 2009 4:22 AM
    Wednesday, December 23, 2009 10:32 PM
  • Yes, thanks. I used a pointer to walkthrough the filenames and added them to a list.
    Wednesday, December 23, 2009 10:41 PM