使用SHFileOperation 复制文件夹

来源:互联网 发布:淘宝手机版卖家中心 编辑:程序博客网 时间:2024/05/17 21:46


#include <Shellapi.h>#pragma comment(lib, "Shell32.lib")
BOOL CopyFolder(LPCTSTR lpszFromPath, LPCTSTR lpszToPath){    SHFILEOPSTRUCT shellFileOp;PTSTR szSrc, szDst;BOOL bRet = FALSE;int iSrcLen, iDstLen;iSrcLen = lstrlen(lpszFromPath);iDstLen = lstrlen(lpszToPath);szSrc = static_cast<PTSTR>(::GlobalAlloc(GPTR, lstrlen(lpszFromPath) + 2));szDst = static_cast<PTSTR>(::GlobalAlloc(GPTR, lstrlen(lpszToPath) + 2));if (szSrc && szDst) {lstrcpy(szSrc, lpszFromPath);szSrc[iSrcLen] = '\0';szSrc[iSrcLen + 1] = '\0';lstrcpy(szDst, lpszToPath);szDst[iDstLen] = '\0';szDst[iDstLen + 1] = '\0';ZeroMemory((void *)&shellFileOp, sizeof(SHFILEOPSTRUCT));shellFileOp.fFlags = FOF_NOCONFIRMATION |FOF_NOCONFIRMMKDIR;shellFileOp.hNameMappings = NULL;shellFileOp.hwnd = NULL;shellFileOp.lpszProgressTitle = NULL;shellFileOp.pFrom = szSrc;shellFileOp.pTo = szDst;shellFileOp.wFunc = FO_COPY;bRet = SHFileOperation(&shellFileOp) == 0;}if (szSrc){::GlobalFree(szSrc);}if (szDst){::GlobalFree(szDst);}return bRet;}

MSDN SHFileOperation function

原创粉丝点击