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

本文详细介绍了文件对话框的使用、目录遍历及删除空目录的实现方法,涉及CFileDialog、CFileFind等类的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

void CDqfView::OnButton1()
{
// TODO: Add your control notification handler code here
CFileDialogdlg(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);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值