1.方法一 :利用CFileFind类作遍历(递归方法)
void BrowseDir( CString strDir, HTREEITEM parent )
{
CFileFind ff;
CString szDir=strDir;
HTREEITEM hSubItem;
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();
hSubItem = m_fileTree.InsertItem( strTitle, 0, 0, parent );
BrowseDir( strPath, hSubItem );
}
else
{
if( !ff.IsDirectory() && !ff.IsDots() )
{
CString strTitle=ff.GetFileTitle();
m_fileTree.InsertItem( strTitle, 0, 0, parent );
}
}
}
ff.Close();
}
2.利用 WIN32_FIND_DATA结构
find(char * lpPath)
{
char szFind[MAX_PATH];
WIN32_FIND_DATA FindFileData;
strcpy(szFind,lpPath);
strcat(szFind,"//*.*");
HANDLE hFind=::FindFirstFile(szFind,&FindFileData);
if(INVALID_HANDLE_VALUE == hFind)
return;
while(TRUE)
{
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(FindFileData.cFileName[0]!='.')
{
strcpy(szFile,lpPath);
strcat(szFile,"//");
strcat(szFile,FindFileData.cFileName);
find(szFile);
}
}
else
{
cout << FindFileData.cFileName;
}
if(!FindNextFile(hFind,&FindFileData))
break;
}
FindClose(hFind);
}
遍历文件夹所有文件的方法
最新推荐文章于 2025-08-10 15:13:44 发布

3482

被折叠的 条评论
为什么被折叠?



