使用SHFileOperation 复制文件夹


#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

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页