void CFileTreeDlg::BrowseFile(int CallNum, CString strFile)
{
CallNum++;
CFileFind ff;
CString szDir = strFile;
if(szDir.Right(1) != "\\")
szDir += "\\";
szDir += "*.*";
BOOL res = ff.FindFile(szDir);
while(res)
{
res = ff.FindNextFile();
if(ff.IsDirectory() && !ff.IsDots())
{
//如果是一个子目录,用递归继续往深一层找
CString strPath = ff.GetFilePath();
CString strTitle = ff.GetFileTitle();
int i =0;
switch(CallNum)
{
case 1:
strHTFir = m_FileTree.InsertItem(strTitle,0,0,NULL);
break;
case 2:
strHTSec = m_FileTree.InsertItem(strTitle,0,0,strHTFir);
break;
case 3:
strHtThi = m_FileTree.InsertItem(strTitle,0,0,strHTSec);
break;
case 4:
strHtFor = m_FileTree.InsertItem(strTitle,0,0,strHtThi);
break;
default:
strHtFif = m_FileTree.InsertItem(strTitle,0,0,strHtFor);
break;
}
BrowseFile(CallNum,strPath);
}
else if(!ff.IsDirectory() && !ff.IsDots())
{
//显示当前访问的文件
CString strPath;
CString strTitle;
strPath = ff.GetFilePath();
strTitle = ff.GetFileTitle();
switch(CallNum)
{
case 1:
strRoot = m_FileTree.InsertItem(strTitle,0,0,NULL);
break;
case 2:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHTFir);
break;
case 3:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHTSec);
break;
case 4:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtThi);
break;
case 5:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtFor);
break;
default:
strHtEnd = m_FileTree.InsertItem(strTitle,0,0,strHtFif);
break;
}
}
}
ff.Close();//关闭
}
使用时调用
BrowseFile(0,“IO表”);//遍历"IO表"文件夹内的所有目录