不用mfc
只能用api
最好能给出代码

解决方案 »

  1.   

    下面的代码为了方便,用了cstring和ctreectrl,
    换掉就行了
    void EnumPath(LPCTSTR lpszPath,CTreeCtrl& tree)
    {
    CString strTmp = lpszPath;
    static HTREEITEM hParent = tree.InsertItem(lpszPath);
    strTmp += "*";
    WIN32_FIND_DATA wfd;
    HANDLE hFind = FindFirstFile(strTmp,&wfd);
    BOOL bContinue = TRUE;
    while(hFind != INVALID_HANDLE_VALUE && bContinue)
    {
    if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && 
    wfd.cFileName[0] != '.')
    {
    HTREEITEM hTemp = hParent;
    hParent = tree.InsertItem(wfd.cFileName,hParent);
    EnumPath(CString(lpszPath) + "\\" + CString(wfd.cFileName) + "\\",tree) ;
    hParent = hTemp;
    }
    else if(wfd.cFileName[0] != '.')
    {
    tree.InsertItem(wfd.cFileName,hParent);
    }
    bContinue = FindNextFile(hFind,&wfd);
    }
    FindClose(hFind);
    }
    调用:
    EnumPath("c:\\tests\\",m_tree);
      

  2.   

    以下是用console的,借用的:
    *****************************************************
    #include<stdio.h>
    #include<windows.h>
    #include<time.h>void SearchFile(LPCSTR path)
    {
    time_t tmStart,tmEnd;
        char *s;
        char File[512];
        HANDLE hFile;
        WIN32_FIND_DATA fd;
        
        strcpy(File, path);
        s = File + strlen(File);
        if (*(s-1) != '\\')
            *s++ = '\\';
        strcpy(s, "*.*");
    time(&tmStart);
    hFile = FindFirstFile(File, &fd);
        if (hFile==INVALID_HANDLE_VALUE)
            return;
        do {
            strcpy(s, fd.cFileName);
            if (fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) {
                //添加对文件夹的处理
                if (*s != '.') //忽略 "." 和 ".."
                    SearchFile(File);
            }
            else { //文件
                //添加对文件的处理
    printf("%s%s\n",File,fd.cFileName );
    Sleep(100);
            }
        }while(FindNextFile(hFile,&fd));
        FindClose(hFile);
    time(&tmEnd);
    printf("Using %ld Seconds\n",tmEnd-tmStart);
    }void main()
    {
    /*
    DWORD drives = 0;
    char buf[128] = {0};

    drives = GetLogicalDrives();
    printf("drives:%x\n",drives); GetLogicalDriveStrings(128,buf);
    printf("Drive string:\t");
    for(char *s = buf; *s; s+=4)
    {
    printf("%s\t",s);
    }
    printf("\n");
    */ char buf[128];
        GetLogicalDriveStrings(128, buf);
        for(char *s = buf; *s; s+=4) {
            SearchFile(s);
        }}
    *******************************************88
      

  3.   

    下面的代码为了方便,用了cstring和ctreectrl,
    换掉就行了
    void EnumPath(LPCTSTR lpszPath,CTreeCtrl& tree)
    {
    CString strTmp = lpszPath;
    static HTREEITEM hParent = tree.InsertItem(lpszPath);
    strTmp += "*";
    WIN32_FIND_DATA wfd;
    HANDLE hFind = FindFirstFile(strTmp,&wfd);
    BOOL bContinue = TRUE;
    while(hFind != INVALID_HANDLE_VALUE && bContinue)
    {
    if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && 
    wfd.cFileName[0] != '.')
    {
    HTREEITEM hTemp = hParent;
    hParent = tree.InsertItem(wfd.cFileName,hParent);
    EnumPath(CString(lpszPath) + "\\" + CString(wfd.cFileName) + "\\",tree) ;
    hParent = hTemp;
    }
    else if(wfd.cFileName[0] != '.')
    {
    tree.InsertItem(wfd.cFileName,hParent);
    }
    bContinue = FindNextFile(hFind,&wfd);
    }
    FindClose(hFind);
    }
    调用:
    EnumPath("c:\\tests\\",m_tree);
      

  4.   

    主要是用FindFirstFile,FindNextFile进行循环调用!上面两个可以试试!
      

  5.   

    遍历指定目录下的所有目录和文件
    void FindAllFile(LPCSTR pszDir)
    {
      WIN32_FIND_DATA ffd ;
      char szDirFile[1024];
      sprintf(szDirFile,"%s*",pszDir);
      HANDLE hFind = FindFirstFile(szDirFile,&ffd);
      if (hFind != INVALID_HANDLE_VALUE)
      {
        char szDir[1024];
        if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
          strcmp(ffd.cFileName,".") && 
          strcmp(ffd.cFileName,".."))
        {
         //确定找到一个目录并且不为 . 或 ..
         //在此处处理目录ffd.cFileName是目录名
         FindAllFile(szDir);
        }
        //在此处处理文件ffd.cFileName是文件名
        
        while(FindNextFile(hFind,&ffd))
        {
          if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY &&
            strcmp(ffd.cFileName,".") && 
            strcmp(ffd.cFileName,".."))
          {
            //确定找到一个目录并且不为 . 或 ..
            //在此处处理目录ffd.cFileName是目录名
            FindAllFile(szDir);
          }
          //在此处处理文件ffd.cFileName是文件名
        }
        FindClose(hFind);
      }
    }
      

  6.   

    to Wargod2002(Wargod2002):
    那两个类用什么换阿,怎么换法?to  mrh123() :
    你的程序可以工作,不过好像没打印出文件夹。to wistaria(听风听雨) :
    我把windows和stdio的头文件加上,在用mrh123()的main函数调用(函数名改为FindAllFile,buf[128]改为buf[1024]),编译、build都没问题,但是运行显示press any key to continue 按键后就退出了。之后我直接用char *s="c:\windows";结果编译都通不过。谢谢各位指教
      

  7.   

    cstring 换成字符串,tree就不要了,直接printf咯