FileDialog选择多个文件,如何得到它们的路径

来源:互联网 发布:淘宝哪家店的零食好吃 编辑:程序博客网 时间:2024/05/11 02:15
void CDqfView::OnButton1()
{
// TODO: Add your control notification handler code here
CFileDialog   dlg(TRUE,    "xls",   NULL,OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT,"Excel file(*.xls)|*.xls");    
if    (IDOK    ==    dlg.DoModal())   
{   
  POSITION pos;   
   pos =    dlg.GetStartPosition();   
  while(pos)   
   {   
   CString    szPathName=dlg.GetNextPathName(pos);
    m_list.AddString(szPathName);
   }   
}  



遍历响应函数

//遍历按钮响应函数
void CBrowseDirDlg::OnBrowse()
{
UpdateData(TRUE);
if(m_strDir=="")
{
    AfxMessageBox("请输入目录");
    return;
}
BrowseDir(m_strDir);
}

//递归函数
void CBrowseDirDlg::BrowseDir(CString strDir)
{
CFileFind ff;
CString szDir = strDir;

if(szDir.Right(1) != "\\")
    szDir += "\\";

szDir += "*.*";

BOOL res = ff.FindFile(szDir);
while(res)
{
    res = ff.FindNextFile();
    if(ff.IsDirectory() && !ff.IsDots()) //IsDirectory() 和ff.IsDots()判断是否是文件夹
    {
     //如果是一个子目录,用递归继续往深一层找
     BrowseDir(ff.GetFilePath());
    }
    else if(!ff.IsDirectory() && !ff.IsDots())
    {
     //显示当前访问的文件
     CStatic* p = (CStatic*)GetDlgItem(IDC_STATIC_FILE);
     CString str;
     str.Format("当前访问的文件:%s",ff.GetFilePath());
     p->SetWindowText(str);
     Sleep(500);
    }
}
ff.Close();//关闭
}

删除不为空的目录

void CDelUnEmptyDirDlg::OnDelDir()
{
UpdateData(TRUE);
RecursiveDelete(m_strDir);
}
void CDelUnEmptyDirDlg::RecursiveDelete(CString szPath)
{
CFileFind ff;
CString path = szPath;

if(path.Right(1) != "\\")
   path += "\\";

path += "*.*";
BOOL res = ff.FindFile(path);

while(res)
{
   res = ff.FindNextFile();
   //是文件时直接删除
   AfxMessageBox(ff.GetFilePath());
   if (!ff.IsDots() && !ff.IsDirectory())
    DeleteFile(ff.GetFilePath());
   else if (ff.IsDots())
    continue;
   else if (ff.IsDirectory())
   {
    path = ff.GetFilePath();
    //是目录时继续递归,删除该目录下的文件
    RecursiveDelete(path);
    //目录为空后删除目录
    RemoveDirectory(path);
   }
}
//最终目录被清空了,于是删除该目录
RemoveDirectory(szPath);

}
原创粉丝点击