前言 經常隨手花上個半到一個小時,自己手寫一個拷貝系統檔案的代碼,裡面用上個幾重遞迴,然後判斷下檔案屬性,看是檔案夾還是檔案。然後自己根據檔案的大小來控制進度條的顯示進度。是否真的需要付出這麼多了?
最近,研究了一下windows shell編程,發現其實很多系統有的功能,系統早就做好,並且完完全全的提供給我們了,只是我們比較喜歡一步步的自己來維護每一個流程。最終導致的結果是,耽誤了開發進度,同時造成了某些不可知的軟體bug。
簡介 在vista之前的版本中,經常可以使用SHFileOperation這個系統函數處理類似於檔案的拷貝、移動、刪除和重新命名操作。但vista之後的版本,系統又提供了com庫介面IFileOperation的方式來處理相同的檔案操作。當然以前的方法依然適用。只不過最新的檔案操作方法使用起來會更加的“人性化”(後面會提到)。
內容
舊版檔案操作拷貝操作
int CEarlyFileOperator::FOCopyFile(const wstring &strFrom, const wstring &strTo){wchar_t srcPath[MAX_PATH];wchar_t dstPath[MAX_PATH];memset(srcPath,'\0',MAX_PATH);memset(dstPath,'\0',MAX_PATH);memcpy(srcPath,strFrom.c_str(),strFrom.length() * sizeof(wchar_t));memcpy(dstPath,strTo.c_str(),strTo.length() * sizeof(wchar_t));SHFILEOPSTRUCT FileOp = {0};FileOp.hwnd = NULL;// 調用過程會改變父視窗屬性FileOp.wFunc = FO_COPY;// 執行檔案拷貝FileOp.pFrom = srcPath;FileOp.pTo = dstPath;FileOp.hNameMappings = NULL;FileOp.fFlags = FOF_ALLOWUNDO;FileOp.lpszProgressTitle = _T("檔案正在拷貝中...");int nRet = SHFileOperation(&FileOp);return nRet;}
移動操作
int CEarlyFileOperator::FORemoveFile(const wstring &strFrom, const wstring &strTo){wchar_t srcPath[MAX_PATH];wchar_t dstPath[MAX_PATH];memset(srcPath,'\0',MAX_PATH);memset(dstPath,'\0',MAX_PATH);memcpy(srcPath,strFrom.c_str(),strFrom.length() * sizeof(wchar_t));memcpy(dstPath,strTo.c_str(),strTo.length() * sizeof(wchar_t));SHFILEOPSTRUCT FileOp = {0};FileOp.hwnd = NULL;// 調用過程會改變父視窗屬性FileOp.wFunc = FO_MOVE;// 執行檔案拷貝FileOp.pFrom = srcPath;FileOp.pTo = dstPath;FileOp.hNameMappings = NULL;FileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;//FileOp.lpszProgressTitle = _T("檔案正在拷貝中...");int nRet = SHFileOperation(&FileOp);return nRet;}
刪除操作
int CEarlyFileOperator::FODelFile(const wstring &strFrom){wchar_t srcPath[MAX_PATH];memset(srcPath,'\0',MAX_PATH);memcpy(srcPath,strFrom.c_str(),strFrom.length() * sizeof(wchar_t));SHFILEOPSTRUCT FileOp = {0};FileOp.hwnd = NULL;FileOp.wFunc = FO_DELETE;FileOp.pFrom = srcPath;FileOp.pTo = NULL;FileOp.hNameMappings = NULL;FileOp.fFlags = FOF_ALLOWUNDO;//允許撤銷,不出現確認對話方塊int nRet = SHFileOperation(&FileOp);return nRet;}
重新命名操作
int CEarlyFileOperator::FORenameFile(const wstring &strFrom, const wstring &strRename){wchar_t srcPath[MAX_PATH];wchar_t reName[MAX_PATH];memset(srcPath,'\0',MAX_PATH);memset(reName,'\0',MAX_PATH);// 擷取路徑地址wstring strPath = strFrom.substr(0,strFrom.rfind(_T("\\")));wstring strTo = strPath + _T("\\") + strRename;memcpy(srcPath,strFrom.c_str(),strFrom.length() * sizeof(wchar_t));memcpy(reName,strTo.c_str(),strTo.length() * sizeof(wchar_t));SHFILEOPSTRUCT FileOp = {0};FileOp.hwnd = NULL;// 調用過程會改變父視窗屬性FileOp.wFunc = FO_RENAME;// 執行檔案拷貝FileOp.pFrom = srcPath;FileOp.pTo = reName;FileOp.hNameMappings = NULL;FileOp.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;int nRet = SHFileOperation(&FileOp);return nRet;}
新版檔案操作拷貝操作
int CLaterFileOperator::FOCopyFile(const wstring &strFrom, const wstring &strTo){HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)){IFileOperation *pfo;///< Create the IFileOperation interfacehr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));if (SUCCEEDED(hr)){///< Set the operation flagshr = pfo->SetOperationFlags(FOF_ALLOWUNDO);if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied source pathIShellItem *psiFrom = NULL;hr = SHCreateItemFromParsingName(strFrom.c_str(), NULL, IID_PPV_ARGS(&psiFrom));if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied destination path.IShellItem *psiTo = NULL;hr = SHCreateItemFromParsingName(strTo.c_str(), NULL, IID_PPV_ARGS(&psiTo));if (SUCCEEDED(hr)){hr = pfo->CopyItem(psiFrom, psiTo, NULL, NULL);if (NULL != psiTo){psiTo->Release();}}psiFrom->Release();}if (SUCCEEDED(hr)){hr = pfo->PerformOperations();} }pfo->Release();}CoUninitialize();}return hr;}
移動操作
int CLaterFileOperator::FORemoveFile(const wstring &strFrom, const wstring &strTo){HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)){IFileOperation *pfo;///< Create the IFileOperation interfacehr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));if (SUCCEEDED(hr)){///< Set the operation flagshr = pfo->SetOperationFlags(FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR);if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied source pathIShellItem *psiFrom = NULL;hr = SHCreateItemFromParsingName(strFrom.c_str(), NULL, IID_PPV_ARGS(&psiFrom));if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied destination path.IShellItem *psiTo = NULL;hr = SHCreateItemFromParsingName(strTo.c_str(), NULL, IID_PPV_ARGS(&psiTo));if (SUCCEEDED(hr)){hr = pfo->MoveItem(psiFrom, psiTo, NULL, NULL);if (NULL != psiTo){psiTo->Release();}}psiFrom->Release();}if (SUCCEEDED(hr)){hr = pfo->PerformOperations();} }pfo->Release();}CoUninitialize();}return hr;}
刪除操作
int CLaterFileOperator::FODelFile(const wstring &strFrom){HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)){IFileOperation *pfo;///< Create the IFileOperation interfacehr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));if (SUCCEEDED(hr)){///< Set the operation flagshr = pfo->SetOperationFlags(FOF_ALLOWUNDO);if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied source pathIShellItem *psiFrom = NULL;hr = SHCreateItemFromParsingName(strFrom.c_str(), NULL, IID_PPV_ARGS(&psiFrom));if (SUCCEEDED(hr)){hr = pfo->DeleteItem(psiFrom, NULL);psiFrom->Release();}if (SUCCEEDED(hr)){hr = pfo->PerformOperations();} }pfo->Release();}CoUninitialize();}return hr;}
重新命名操作
int CLaterFileOperator::FORenameFile(const wstring &strFrom, const wstring &strRename){HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)){IFileOperation *pfo;///< Create the IFileOperation interfacehr = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pfo));if (SUCCEEDED(hr)){///< Set the operation flagshr = pfo->SetOperationFlags(FOF_ALLOWUNDO | FOF_NOCONFIRMATION);if (SUCCEEDED(hr)){///< Create an IShellItem from the supplied source pathIShellItem *psiFrom = NULL;hr = SHCreateItemFromParsingName(strFrom.c_str(), NULL, IID_PPV_ARGS(&psiFrom));if (SUCCEEDED(hr)){hr = pfo->RenameItem(psiFrom, strRename.c_str(), NULL);psiFrom->Release();}if (SUCCEEDED(hr)){hr = pfo->PerformOperations();} }pfo->Release();}CoUninitialize();}return hr;}