枚举文件夹,枚举指定文件夹;

一:枚举文件夹函数:


void SpecialFolder_Browse(HWND hwnd)

 
 LPMALLOC g_pMalloc; 
 
 /* Gets the Shell's default allocator */ 
 
 if (::SHGetMalloc(&g_pMalloc) == NOERROR) 
  
 { 
  
        BROWSEINFO bi;  
  
        LPSTR lpBuffer;  
  
        LPITEMIDLIST pidlSpecialFolder;  // PIDL for Special Folder 
  
        LPITEMIDLIST pidlBrowse;    // PIDL selected by user  
  
  
  
        // Allocate a buffer to receive browse information.  
  
        if ((lpBuffer = (LPSTR) g_pMalloc->Alloc(MAX_PATH)) != NULL)     
  {  
   

   
   // Get the PIDL for the Programs folder.  
   
   if (SUCCEEDED(SHGetSpecialFolderLocation(hwnd, CSIDL_DRIVES , &pidlSpecialFolder)))      //第二个参数可选;
   {  
    
    // Fill in the BROWSEINFO structure.  
    
     bi.hwndOwner = hwnd;  
    
    bi.pidlRoot = pidlSpecialFolder;  
    
    bi.pszDisplayName = lpBuffer;  
    
    bi.lpszTitle = "Choose a Program Group";  
    
    bi.ulFlags = 0;  
    
    bi.lpfn = NULL;  
    
    bi.lParam = 0;  
    
    
    
    // Browse for a folder and return its PIDL.  
    
    pidlBrowse = SHBrowseForFolder(&bi);  
    
    if (pidlBrowse != NULL)  
     
    {   
     
                    // Show the display name, title, and file system path. 
   

                    MessageBox(hwnd, lpBuffer, "Display name", MB_OK);      //点击确定后显示选择的文件夹;
        
        if (SHGetPathFromIDList(pidlBrowse, lpBuffer))  
        
         SetWindowText(hwnd, lpBuffer);   
         MessageBox(hwnd, lpBuffer, "Display full name", MB_OK); //显示全部路径;
        
        
        // Free the PIDL returned by SHBrowseForFolder.  
       
        g_pMalloc->Free(pidlBrowse);  
       
    }  
    
    
    
    // Clean up.  
    
    g_pMalloc->Free(pidlSpecialFolder);  
    
   } 
   
   g_pMalloc->Free(lpBuffer);  
   
  } 
  
  // Release the shell's allocator. 
  
  g_pMalloc->Release(); 
  
 } 
 

 

 

二:枚举指定文件夹:


#include   <shlobj.h>
#include <iostream>
using namespace std;

void   EnumerateFolder(LPCTSTR   path)
{
 HRESULT   hr;   //   COM   result,   you 'd   better   examine   it   in   your   code!
 hr   =   CoInitialize(NULL);   //   initialize   COM
 //   NOTE:   usually   COM   would   be   initialized   just   once   in   your   main()
 
 LPMALLOC   pMalloc   =   NULL;   //   memory   manager,   for   freeing   up   PIDLs
 hr   =   SHGetMalloc(&pMalloc);
 
 LPSHELLFOLDER   psfDesktop   =   NULL;   //   namespace   root   for   parsing   the   path
 hr   =   SHGetDesktopFolder(&psfDesktop);
 
 //   IShellFolder::ParseDisplayName   requires   the   path   name   in   Unicode.
 OLECHAR   olePath[MAX_PATH];   //   wide-char   version   of   path   name   
 MultiByteToWideChar(CP_ACP,   MB_PRECOMPOSED,   path,   -1,   olePath,   MAX_PATH);
 
 //   parse   path   for   absolute   PIDL,   and   connect   to   target   folder
 LPITEMIDLIST   pidl   =   NULL;   //   general   purpose
 hr   =   psfDesktop-> ParseDisplayName(NULL,   NULL,   olePath,   NULL,   &pidl,   NULL);
 LPSHELLFOLDER   psfFolder   =   NULL;
 hr   =   psfDesktop-> BindToObject(pidl,   NULL,   IID_IShellFolder, (void**)&psfFolder);  
  
 psfDesktop-> Release();   //   no   longer   required
 pMalloc-> Free(pidl);
 
 LPENUMIDLIST   penumIDL   =   NULL;   //   IEnumIDList   interface   for   reading   contents
 hr   =   psfFolder-> EnumObjects(NULL,   SHCONTF_FOLDERS   |   SHCONTF_NONFOLDERS,  &penumIDL); 
 
 while(1) 
 {
  //   retrieve   a   copy   of   next   local   item   ID   list
  hr   =   penumIDL-> Next(1,   &pidl,   NULL);
  if(hr   ==   NOERROR) 
  {
   WIN32_FIND_DATA   ffd;   //   let 's   cheat   a   bit   :)
   hr   =   SHGetDataFromIDList(psfFolder,   pidl,   SHGDFIL_FINDDATA,   &ffd,   sizeof(WIN32_FIND_DATA));
   
   cout <<   "Name   =   "   <<   ffd.cFileName<<   endl;     
   cout <<   "Type   =   "   <<   (   (ffd.dwFileAttributes   &   FILE_ATTRIBUTE_DIRECTORY)  ?   "dir\n "   :   "file\n "   );
   cout <<   "Size   =   "   <<   ffd.nFileSizeLow   <<   endl;
   
   pMalloc-> Free(pidl);
  }
  //   the   expected   "error "   is   S_FALSE,   when   the   list   is   finished
  else   break;
 }
 
 //   release   all   remaining   interface   pointers
 penumIDL-> Release();
 psfFolder-> Release();
 pMalloc-> Release();
 
 CoUninitialize();   //   shut   down   COM
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值