调用Windows api遍历文件夹下子文件的路径

void TraverseDir( CString strDir, CAtlArray<CString>& arrFilePaths)
{
    HANDLE hFile;
    if (!DataConvert::StringEqual(strDir.Right(1), _T("\\")))
        strDir.Append(_T("\\"));
    CString strSearch = strDir+ _T("*.*");
    LPCTSTR lpFileName(strSearch);    
    WIN32_FIND_DATA pNextInfo;    
    hFile = FindFirstFile(lpFileName, &pNextInfo);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        //搜索失败
        exit(-1);
    }
    while (FindNextFile(hFile, &pNextInfo))
    {
        if (pNextInfo.cFileName[0] == '.')//过滤.和..
            continue;
        CString strFilePath;
        strFilePath.Format(_T("%s"), pNextInfo.cFileName);
        arrFilePaths.Add(strDir + strFilePath);
    }
}

 

posted @ 2022-07-23 16:39  香菇0_0  阅读(85)  评论(0)    收藏  举报