Win32 API SHFileOperation
The win32 API SHFileOperation can be used to delete a whole directory or copy a directory from a path to another, below is an example:
// Delete the whole directory
BOOL DelDir(LPCTSTR lpszPath)
{
char fPath[MAX_PATH];
memset(fPath, 0, sizeof(fPath));
strcpy(fPath, lpszPath);
// Note : 目录不能以\或/结尾
for (int i = strlen(fPath) - 1; i >= 0; i--) {
if (fPath[i] == '\\' || fPath[i] == '/') {
fPath[i] = 0;
} else {
break;
}
}
SHFILEOPSTRUCT FileOp;
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = fPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
return SHFileOperation(&FileOp) == 0;
}
// Copy Directory or file, 注意字符串fromPath与toPath必须以"\0\0"结尾,
// 且目录的文件分割符是'\',不是'/'
BOOL copy(LPCTSTR fromPath, LPCTSTR toPath)
{
SHFILEOPSTRUCT FileOp;
FileOp.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = fromPath;
FileOp.pTo = toPath;
FileOp.wFunc = FO_COPY;
return SHFileOperation(&FileOp) == 0;
}
没有评论:
发表评论