请问各位,我想得到一个目录下面的各个子目录和文件,并把它保存到一个变量里面,该怎么做啊

解决方案 »

  1.   

    用 CFileFind.搞个例子看看吧
      

  2.   

    给你一个现成的例子,我以前写的:
    void ScanDiskFile(CString m_driverflag)//参数为驱动器如C:
    {
      FindNeedfile(m_driverflag);        
      Recurse(m_driverflag);
    }
    //*********************************************************
    //
    //void FindNeedfile(CString m_driverflag)
    //输入:CString m_driverflag
    //m_driverflag: 硬盘驱动符
    //输出:无
    //功能:找到当前目录下的所需文件//
    //********************************************************
    void FindNeedfile(CString m_driverflag)
    {
      CFileFind finder;  
      CString strWildcard;
      strWildcard=m_driverflag+_T("\\*.*");  BOOL fFinding = finder.FindFile(strWildcard);
      while(fFinding)
      {
         fFinding=finder.FindNextFile();
         if(!finder.IsDirectory())
         {
           ....//这里是所有目录下的文件
         }
      }
      finder.Close();
    }//********************************************************
    //
    //void Recurse(CString m_driverflag)
    //输入:CString m_driverflag
    //m_driverflag:驱动器符
    //输出:无
    //功能:遍历某一驱动器下所有文件
    //
    //*********************************************************
    void Recurse(CString m_driverflag)
    {
       CFileFind finder;   CString strWildcard(m_driverflag);
       strWildcard += _T("\\*.*");   // 开始遍历所有文件
       BOOL bWorking = finder.FindFile(strWildcard);   while (bWorking)
       {
          bWorking = finder.FindNextFile();      if (finder.IsDots()) //如果是.或..则继续不计
             continue;      // 如果是目录的话,则进入遍历      if (finder.IsDirectory())
          {
             
            CString strpath = finder.GetFilePath();
            FindNeedfile(strpath);
            Recurse(strpath);
          }
       }   finder.Close();}
      

  3.   

    FindFirstFile
    The FindFirstFile function searches a directory for a file whose name matches the specified filename. FindFirstFile examines subdirectory names as well as filenames. HANDLE FindFirstFile(
      LPCTSTR lpFileName,  // pointer to name of file to search for
      LPWIN32_FIND_DATA lpFindFileData 
                           // pointer to returned information
    );
     
    Parameters
    lpFileName 
    Windows 95: Pointer to a null-terminated string that specifies a valid directory or path and filename, which can contain wildcard characters (* and ?). This string must not exceed MAX_PATH characters. 
    Windows NT: Pointer to a null-terminated string that specifies a valid directory or path and filename, which can contain wildcard characters (* and ?). There is a default string size limit for paths of MAX_PATH characters. This limit is related to how the FindFirstFile function parses paths. An application can transcend this limit and send in paths longer than MAX_PATH characters by calling the wide (W) version of FindFirstFile and prepending "\\?\" to the path. The "\\?\" tells the function to turn off path parsing; it lets paths longer than MAX_PATH be used with FindFirstFileW. However, each component in the path cannot be more than MAX_PATH characters long. This also works with UNC names. The "\\?\" is ignored as part of the path. For example, "\\?\C:\myworld\private" is seen as "C:\myworld\private", and "\\?\UNC\bill_g_1\hotstuff\coolapps" is seen as "\\bill_g_1\hotstuff\coolapps". lpFindFileData 
    Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory. The structure can be used in subsequent calls to the FindNextFile or FindClose function to refer to the file or subdirectory. FindNextFile
    The FindNextFile function continues a file search from a previous call to the FindFirstFile function. BOOL FindNextFile(
      HANDLE hFindFile,  // handle to search
      LPWIN32_FIND_DATA lpFindFileData 
                         // pointer to structure for data on found file
    );
     
    Parameters
    hFindFile 
    A search handle returned by a previous call to the FindFirstFile function. 
    lpFindFileData 
    Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory. The structure can be used in subsequent calls to FindNextFile to refer to the found file or directory. 
    Return Values
    If the function succeeds, the return value is nonzero.If the function fails, the return value is zero. To get extended error information, call GetLastError. If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES. Res
    The FindNextFile function searches for files by name only; it cannot be used for attribute-based searches.