標籤:sys 複製 security amp 問題 plain pst bool 軟體
最近需要在Windows下拷貝大量小檔案(數量在十萬層級以上)。寫了些拷貝檔案的小程式,竟然發現不同的選擇,拷貝的速度有天壤之別!
現有這樣的測試資料:1500+小檔案,總大小10M左右。現用不同方法進行拷貝。:
方案1:調用SHFileOperation
[cpp] view plain copy
- BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
- {
- size_t nLengthFrm = _tcslen(lpszFromPath);
- TCHAR *NewPathFrm = new TCHAR[nLengthFrm+2];
- _tcscpy(NewPathFrm,lpszFromPath);
- NewPathFrm[nLengthFrm] = ‘\0‘;
- NewPathFrm[nLengthFrm+1] = ‘\0‘;
-
- SHFILEOPSTRUCT FileOp;
- ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
- FileOp.fFlags = FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_NOERRORUI|FOF_FILESONLY|FOF_NOCOPYSECURITYATTRIBS ;
- FileOp.hNameMappings = NULL;
- FileOp.hwnd = NULL;
- FileOp.lpszProgressTitle = NULL;
- FileOp.pFrom = NewPathFrm;
- FileOp.pTo = lpszToPath;
- FileOp.wFunc = FO_COPY;
- return return SHFileOperation(&FileOp);
- }
代碼比較羅索。複製完成用時:57,923毫秒。
方案2:調用API:CopyFile
[cpp] view plain copy
- BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
- {
- return CopyFile(lpszFromPath, lpszToPath, TRUE);
- }
代碼短小精悍。複製用時:700毫秒。
方案3:調用CMD命令。
[cpp] view plain copy
- BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
- {
- TCHAR tbuff[255];
- char buff[255];
- _stprintf(tbuff, _T("copy /Y %s %s"), lpszFromPath, lpszToPath);
- TChar2Char(tbuff, buff, 255);
- system(buff);
- return TRUE;
- }
跑到5分鐘後直接卡死。。沒有得出結果,可能是參數傳遞的問題。
http://blog.csdn.net/lsldd/article/details/8191338
windows程式中拷貝檔案的選擇