请问如何搜索文件夹下的所有文件包括子文件夹?请问采用什么方法呢?在MFC中有现成的函数吗?

解决方案 »

  1.   

    CStringArray m_strArrFilename;
    void CTest6Dlg::OnButton1()
    {
    FindMyFile("d:\\temp\\"); CString strtemp; for(int i=0; i<m_strArrFilename.GetSize(); i++) { strtemp += m_strArrFilename[i] + "\r\n"; } AfxMessageBox(strtemp);
    }void CTest6Dlg::FindMyFile(CString path){    BOOL bFind, bFindSuffix; CFileFind tempFind, tempFind1; _chdir(path); bFind = tempFind.FindFile("*.*"); while(bFind) { bFind = tempFind.FindNextFile(); if(tempFind.IsDirectory()) { if (!tempFind.IsDots() ) { CString temppath; temppath = tempFind.GetFilePath(); FindMyFile(temppath);  } } } _chdir(path); bFindSuffix = tempFind1.FindFile("*.*"); while(bFindSuffix) { bFindSuffix = tempFind1.FindNextFile(); CString filepath, filename; if( !tempFind1.IsDirectory() && !tempFind1.IsDots()) { //得到目录下所有文件 filepath = tempFind1.GetFilePath(); m_strArrFilename.Add(filepath); } } tempFind.Close(); tempFind1.Close();}
      

  2.   

    void Recurse(LPCTSTR pstr)
    {
       CFileFind finder;   // build a string with wildcards
       CString strWildcard(pstr);
       strWildcard += _T("\\*.*");   // start working for files
       BOOL bWorking = finder.FindFile(strWildcard);   while (bWorking)
       {
          bWorking = finder.FindNextFile();      // skip . and .. files; otherwise, we'd
          // recur infinitely!      if (finder.IsDots())
             continue;      // if it's a directory, recursively search it      if (finder.IsDirectory())
          {
             CString str = finder.GetFilePath();
             cout << (LPCTSTR) str << endl;
             Recurse(str);
          }
       }   finder.Close();
    }