请问,有什么函数或者控件可以逐一读出一个目录下的所有文件名?谢谢!

解决方案 »

  1.   

    void CMyComDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
    AddList("C:\\myDir");}
    void CMyComDlg::AddList(char* DirName)
    {
    CFileFind tempFind;
    char tempFileFind[200];
    sprintf(tempFileFind,"%s\\*.*",DirName);
    BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
    while(IsFinded)
    {
    IsFinded=(BOOL)tempFind.FindNextFile();
    if(!tempFind.IsDots())
    {
    char foundFileName[200];
    strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));

    //还有子目录,子目录下的所有文件加入list
    if(tempFind.IsDirectory())
    {
    char tempDir[200];
    sprintf(tempDir,"%s\\%s",DirName,foundFileName);
                AddList(tempDir);   
    }
    else
    {
    CString StrAdd;
    CString strFileName=tempFind.GetFileName();
    //去掉扩展名
    int EXTNameBegin = strFileName.Find('.');
    if(EXTNameBegin==-1)
    {
    StrAdd=strFileName;
    }
    else
    {
    StrAdd=strFileName.Mid(0,EXTNameBegin);
    }

    //文件加入列表
    m_Comb.AddString(StrAdd);
    } }
    }
    tempFind.Close();
    }
      

  2.   

    删除目录及目录下所有文件与子目录
    (Hermess发表于2002-5-24 22:10:27)
      VC++只提供了删除一个空目录的函数,而在实际应用中往往希望删除其下有很多子目录与文件的目录。为了实现这一功能,我编写了DeleteDirectory 
    函数,它可以实现这一功能。函数原型:BOOL DeleteDirectory(char *DirName);
    返回值:成功删除时返回TRUE,否则返回FALSE
    参数DirName为要删除的目录名,必须为绝对路径名,如“c:\\temp"。函数定义如下:
    BOOL DeleteDirectory(char *DirName)
    {
       CFileFind tempFind;
       char tempFileFind[200];
       sprintf(tempFileFind,"%s\\*.*",DirName);
       BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
       while(IsFinded)
       {
          IsFinded=(BOOL)tempFind.FindNextFile();
          if(!tempFind.IsDots())
          {
             char foundFileName[200];
             strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
             if(tempFind.IsDirectory())
             {
                char tempDir[200];
                sprintf(tempDir,"%s\\%s",DirName,foundFileName);
                DeleteDirectory(tempDir);
             }
             else
             {
                char tempFileName[200];
                sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
                DeleteFile(tempFileName);
             }
          }
       }
       tempFind.Close();
       if(!RemovwDirctory(DirName))
       {
          MessageBox(0,"删除目录失败!","警告信息",MK_OK);
          return FALSE;
       }
       return TRUE;
    }
      

  3.   

    HANDLE hFind;
    WIN32_FIND_DATA dataFind;
    BOOL bMoreFiles=TRUE;
    CString strFile;hFind=FindFirstFile(m_strMainDir+"\\*.*",&dataFind);while(hFind!=INVALID_HANDLE_VALUE && bMoreFiles ==TRUE)
    {
     if(dataFind.dwFileAttributes ==FILE_ATTRIBUTE_ARCHIVE)
     {
      int nChar =dataFind.cFileName[0];
      if(islower(nChar))
       nChar-=32;
      if(isalpha(nChar))
       nChar-='A';
      else
       nChar=26;
      m_treeFiles.InsertItem(dataFind.cFileName,hLetter[nChar];//在树中插入文件名
     }
     bMoreFiles=FindNextFile(hFind,&dataFind);
    }
    FindClose(hFind);
      

  4.   

    int Cselectfile::GetDetailFilePath(CString csFilePath)
    {
    WIN32_FIND_DATA fd; 
    HANDLE  m_handle;
    CString  csFileName;
    //int      dbfflag;
    csFileName=csFilePath+"\\*.*";
    //csFilePath+="/";
    m_handle=::FindFirstFile(csFileName,&fd);
    while(1)
    {
    if ((strcmp(fd.cFileName,".")==0 )||(strcmp(fd.cFileName,"..")==0))
    {
    if(!::FindNextFile(m_handle,&fd))
        break;
    continue;
    }
    else if(fd.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
    {
                GetDetailFilePath(csFilePath+"\\"+fd.cFileName);
    if(!::FindNextFile(m_handle,&fd))
        break;
    continue;
    }
    else
    {
    //tempstr=fd.cFileName;
    //dbfflag=tempstr.Find(".dbf");
    //if(dbfflag>0)
    //{
    csProFileName[FileCount]=csFilePath+"\\"+fd.cFileName;
    FileCount++;
                //}
    if(!::FindNextFile(m_handle,&fd))
    break;
    continue;
    } }
    return 1;}