FileDialog选择多个文件,如何得到它们的路径
2012-03-07 22:57
453 查看
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);
}
{
// 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);
}
相关文章推荐
- 如何使用 OpenFileDialog 组件 (选择文件组件)
- 1、怎样设置C#OpenFileDialog(文件选择窗体)的指定路径、文件格式等属性(设置打开默认路径、文件格式、窗体显示文本)
- C# OpenFileDialog 打开/选择文件对话框
- 使用OpenFileDialog选择文件
- FireFox中得到file控件取到客户端文件的完整路径
- 文件选择对话框filedialog,OpenFileDialog,FolderBrowserDialog
- C# 如何提取SaveFileDialog的保存路径
- RCP编程技巧:如何得到文件路径
- 如何让file input能够实现置空和【异步】上传后相同文件的再次选择触发change
- Qt-编程模块 001: 浏览(选择)、打开 文件和文件夹(目录) --- 如何让按钮**记忆上次打开的文本路径**的功能
- C#中OpenFileDialog获取文件名和文件路径的常用方法
- JAVA中如何得到文件路径( 转)
- 如何用c#打开文件夹并选择文件夹内的一个文件 open folder and select file
- 利用javascript使html的file不可手动输入文件路径,只可通过[浏览]按钮选择
- 已知某个文件的全部路径,如d:\aa\bbb\ccc\0.jpg 如何得到0.jpg的父目录名称cc呢?
- How To Get Application Path,And How To Get The Path's Directory,File Name,And Type Info(如何得到运行程序的路径,以及如何得到路径的文件夹,文件名,以及类型等等信息)
- 如何从窗口句柄得到与窗口关联的文件路径/文件名
- 如何得到通过GetOpenFileName选择的多个文件的文件名
- 如何用c#打开文件夹并选择文件夹内的一个文件 open folder and select file
- C# savefiledialog 保存文件后 再操作数据库 提示数据库文件路径错误