Hello I try write program to开发者_JAVA百科 create and copy some files.
If folder is not exist, when i use flag
"SH.fFlags |= FOF_SILENT;
SH.fFlags |= FOF_NOCONFIRMMKDIR;" at the same time, can't create folder and can't copy file.
Any body know why? Thanks.
CString source;
CString target;
SHFILEOPSTRUCT SH = { 0 };
SH.hwnd = NULL;
SH.wFunc = FO_COPY;
SH.fFlags = NULL;
SH.fFlags |= FOF_SILENT;
SH.fFlags |= FOF_NOCONFIRMMKDIR;
SH.fFlags |= FOF_NOCONFIRMATION;
SH.fFlags |= FOF_WANTMAPPINGHANDLE;
SH.fFlags |= FOF_NOERRORUI;
source = _T("c:\\Test\\test1\\Test1.exe");
target = _T("C:\\Backup\\Test\\");
source += '\0';
target += '\0';
SH.pTo = target;
SH.pFrom = source;
::SHFileOperation( &SH );
CString
does not support embedded zeroes. You need to copy the string to e.g. a vector:
(I'm not sure if this is the only problem with your code, but you should fix it):
CString source;
std::vector<TCHAR> sourceBuffer;
sourceBuffer.resize(source.GetLength()+1);
memcpy( &(sourceBuffer[0]), source.operator LPCTSTR(),
sizeof(TCHAR) * (source.GetLength()+1)); // (1)
sourceBuffer.push_back('\0');
// do the same for target / targetBuffer
SH.pTo = &(targetBuffer[0]);
SH.pFrom = &(sourceBuffer[0]);
(1) the magic line:
&(sourceBuffer[0])
is a pointer to the first character of the vector buffer we just allocated, and the target of the memcpy operationsource.operator LPCTSTR()
is an explicit call to the (otherwise implicit) conversion to a LPCTSTR, which gives the source of the memcpy operationsource.GetLength() + 1
is the length of the string in characters, including the terminating zero- sizeof(TCHAR) is the size of a character in bytes (usually 1 in ANSI builds, 2 in Unicode builds). memcpy expects number of bytes, so we need to multiply by that.
精彩评论