windows程式中拷貝檔案的選擇

來源:互聯網
上載者:User

標籤:sys   複製   security   amp   問題   plain   pst   bool   軟體   

最近需要在Windows下拷貝大量小檔案(數量在十萬層級以上)。寫了些拷貝檔案的小程式,竟然發現不同的選擇,拷貝的速度有天壤之別!

現有這樣的測試資料:1500+小檔案,總大小10M左右。現用不同方法進行拷貝。:

方案1:調用SHFileOperation

 

[cpp] view plain copy 
  1. BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)  
  2. {  
  3.     size_t nLengthFrm = _tcslen(lpszFromPath);  
  4.     TCHAR *NewPathFrm = new TCHAR[nLengthFrm+2];  
  5.     _tcscpy(NewPathFrm,lpszFromPath);  
  6.     NewPathFrm[nLengthFrm] = ‘\0‘;  
  7.     NewPathFrm[nLengthFrm+1] = ‘\0‘;  
  8.   
  9.     SHFILEOPSTRUCT FileOp;  
  10.     ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));  
  11.     FileOp.fFlags = FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_NOERRORUI|FOF_FILESONLY|FOF_NOCOPYSECURITYATTRIBS ;  
  12.     FileOp.hNameMappings = NULL;  
  13.     FileOp.hwnd = NULL;  
  14.     FileOp.lpszProgressTitle = NULL;  
  15.     FileOp.pFrom = NewPathFrm;  
  16.     FileOp.pTo = lpszToPath;  
  17.     FileOp.wFunc = FO_COPY;  
  18.     return return SHFileOperation(&FileOp);  
  19. }  


代碼比較羅索。複製完成用時:57,923毫秒。

 

方案2:調用API:CopyFile

 

[cpp] view plain copy 
  1. BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)  
  2. {  
  3.     return CopyFile(lpszFromPath, lpszToPath, TRUE);  
  4. }  

代碼短小精悍。複製用時:700毫秒。

方案3:調用CMD命令。

 

[cpp] view plain copy 
  1. BOOL CUtility::CopyFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)  
  2. {  
  3.     TCHAR tbuff[255];  
  4.     char  buff[255];  
  5.     _stprintf(tbuff, _T("copy /Y %s %s"), lpszFromPath, lpszToPath);  
  6.     TChar2Char(tbuff, buff, 255);  
  7.     system(buff);  
  8.     return TRUE;  
  9. }  

 

 

跑到5分鐘後直接卡死。。沒有得出結果,可能是參數傳遞的問題。

 

http://blog.csdn.net/lsldd/article/details/8191338

windows程式中拷貝檔案的選擇

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.