Link to home
Start Free TrialLog in
Avatar of syu
syu

asked on

Get file names from a multiple select open dialog box

I have a multiple select open dialog box, and I would like to get file names from the File Name edit box.  I use the following code, but what I got from GetNextPathName( position ) is an empty string...


POSITION position = GetStartPosition( );
CString pFileName = GetNextPathName( position );
while ( pFileName != "" )
{
    ...
    pFileName = GetNextPathName( position );
}

Is there any wrong with this code?  I have set the OFN_ALLOWMULTISELECT flag.  Thanks.
Avatar of syu
syu

ASKER

Edited text of question
Here is my code that works. ChooseFile is the name of the dialog.
Be sure that your buffer (m_ofn.lpstrFile ) is initialized properly (see below) :

      char *szChosenFiles = new char[150*MAX_PATH];
      if(!szChosenFiles)
            return;

      strcpy(szChosenFiles,_T(""));
      ChooseFile.m_ofn.lpstrFile = szChosenFiles;
      ChooseFile.m_ofn.nMaxFile = 150*MAX_PATH;
      if(ChooseFile.DoModal() == IDOK){
                              POSITION  Position =  ChooseFile.GetStartPosition();
               while(Position != NULL){
                  strFileName = ChooseFile.GetNextPathName(Position);
                                 // do what you want
                }

Avatar of syu

ASKER

I forgot to mention that I put my code on the OnFileNameChange function because I need file names when the Open dialog box is still active.  I setup the lpstrFile and nMaxFile before calling the DoModal( ), but it still doesn't work.  
After GetNextPathName( position ), position is always 0x00000000.
Is there any reason why it doesn't work?
Thanks.
CFileDialog::GetNextPathName can only be used after DoModal().
Avatar of syu

ASKER

I figured that out.  But is there any way I can get the file names in the class?  I got the control of the File Name edit box and got the file names in one CString.  Well, is there any way I can get each file name out from that CString?  I am thinking to get the control of the List View to get the current selected file name.
Thanks.

ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial