现在的位置: 首页 > 综合 > 正文

使用SHFileOperation 复制文件夹

2013年03月08日 ⁄ 综合 ⁄ 共 948字 ⁄ 字号 评论关闭

#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

抱歉!评论已关闭.