现在的位置: 首页 > 综合 > 正文

MFC读取多文件与文件夹

2013年10月04日 ⁄ 综合 ⁄ 共 3493字 ⁄ 字号 评论关闭

/*********************************************************
函数名称:ReadFile
功能描述:打开文件对话框,读入文件
参数说明:
返 回 值:无
*********************************************************/
void ReadFile()//读入文件,单张图片或多张
{
 CString filepath;

  //这里使用图片格式,读者可以改成自己需要的文件格式。其中OFN_ALLOWMULTISELECT是表示可以多选
 CFileDialog dlg(true, NULL, NULL,OFN_ALLOWMULTISELECT, "(*.jpg)|*.jpg||*", NULL );

 if(IDCANCEL!=dlg.DoModal())
 {
 
  POSITION pos=dlg.GetStartPosition();

  while (pos!=NULL)
  {
   CString path=dlg.GetNextPathName(pos);//路径名
   if(::GetFileAttributes(path)!=-1)
   {
     CString picname=path.Mid(path.ReverseFind('\\')+1);//提取文件
        PushToList(picname,path);//保存文件名和文件路径
   }
 
 
  }
 }
 
}
////////////导入文件夹///////////////////
void CAddTodlg::OnLoadfolder()
{
 // TODO: Add your command handler code here
 char lpszCurDir[100];
 GetCurrentDirectory(strlen(lpszCurDir), lpszCurDir); //因为打开对话框后会改变当前路径,可能会出现其

他错误,所以
 //用 SetCurrentDirectory来设置。
 static TCHAR strDirName[MAX_PATH];
 
 BROWSEINFO bi;
 
 CString szString = TEXT("选择一个源文件子文件夹");
 
 bi.hwndOwner = ::GetFocus();
 
 bi.pidlRoot = NULL;
 
    bi.pszDisplayName = strDirName;
 
 bi.lpszTitle = szString;
 
    bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS;
 
    bi.lpfn = NULL;
 
 bi.lParam = 0;
 
 bi.iImage = 0;
 
 LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&bi);
 
 if(pItemIDList == NULL)
  
 {
  
  return ;
  
 }
 
 ::SHGetPathFromIDList(pItemIDList, strDirName);
 
 CString str = strDirName;
 
 if(str != "" && str.Right(1) != '\\')
  
  str += '\\';
      FindWholeFile(str);
 
 SetCurrentDirectory(lpszCurDir);
}
/*********************************************************
函数名称:FindWholeFile
功能描述:对文件夹进行遍历查找,插到CImageList,和CListCtrl表
参数说明:pathstr ,文件夹路径,
返 回 值:无
*********************************************************/
void FindWholeFile(CString pathStr)//导入文件夹
{
 CString myDataPath,fdPath,picturename="",pathname="";
    myDataPath=pathStr + "\\*";
 
 CFileFind find;
 BOOL bflag = find.FindFile(myDataPath);
 while(bflag)
 {                       
  bflag = find.FindNextFile();
  if(!find.IsDots())
  {
   fdPath=find.GetFilePath();
  
   if (find.IsDirectory())
   {
    //如果是文件夹,递归,继续往下找                       
    FindWholeFile(fdPath);
   }
   else
   {
    //如果是文件
   
    CString p1=find.GetFileName();
  
    CString p2=p1.Mid(p1.ReverseFind('.')+1);
    if(p2=="jpg")
    {
     picturename = p1;
     pathname = fdPath;
     PushToList(picturename,pathname);
  
    }
    
   }
  }
 }
 find.Close();
}
这里随便说一下怎样创建文建夹,就是你想导出文件夹的话
/*********************************************************
函数名称:getFloderPath
功能描述:得到用户选择的文件夹
参数说明:strPath ,选择的文件夹路径
返 回 值:bool,真为确定,假为取消
*********************************************************/
bool getFloderPath(CString &strPath)
{

 

 TCHAR chPath[MAX_PATH]; //用来存储路径的字符串
    BROWSEINFO bInfo;
    GetModuleFileName(NULL,chPath,MAX_PATH);
    strPath=chPath;
    ZeroMemory(&bInfo, sizeof(bInfo));
    bInfo.hwndOwner = ::AfxGetMainWnd()->m_hWnd;
    bInfo.lpszTitle = _T("请选择路径: ");   
    bInfo.ulFlags   = BIF_RETURNONLYFSDIRS|0x40;
    bInfo.lParam    = (LPARAM)strPath.GetBuffer(strPath.GetLength());
   
    LPITEMIDLIST lpDlist; //用来保存返回信息的IDList
    lpDlist = SHBrowseForFolder(&bInfo) ; //显示选择对话框
    strPath=_T("");
    if(lpDlist != NULL)  //用户按了确定按钮
    {
        SHGetPathFromIDList(lpDlist, chPath);//把项目标识列表转化成字符串
        strPath.Format(_T("%s"),chPath); //将TCHAR类型的字符串转换为CString类型的字符串
       return true;
    }

  return false;

}
最后要注意的是在调用这些打开文件或文件夹最好用
char lpszCurDir[100];
GetCurrentDirectory(strlen(lpszCurDir), lpszCurDir);
SetCurrentDirectory(lpszCurDir);
来还原当前路径,否则会出现一些错误,例如当你项目需要连接数据库的时候(你用的是相对路径),这时候会连接不上了

,因为相对路径已经改了。所以我们可以在开始用
char lpszCurDir[100];
GetCurrentDirectory(strlen(lpszCurDir), lpszCurDir);
//进行你要的操作


最后还原
SetCurrentDirectory(lpszCurDir);
 

 

抱歉!评论已关闭.