这会在桌面上创建一个新文件夹,但不会将文件夹 .pfrom 的内容移动到文件夹 .pTo。
int main()
{
SHFILEOPSTRUCT sf = {0};
TCHAR myt[MAX_PATH];
GetModuleFileName(NULL, myt, MAX_PATH); // puts the currente exe path in the buffer myt
string currentexepath;
int i;
for(i = 0; myt[i] != NULL; i++) { // this loop is for converting myt to string
currentexepath += myt[i]; // because string capabilities are needed
}
i = currentexepath.find_last_of("\\/");
currentexepath = currentexepath.substr(0, i);
currentexepath += "\\subfolder\\*.*\0"; //i tried with and without *.* and \0
wstring ws = s2ws(currentexepath);
sf.wFunc = FO_COPY;
sf.hwnd = 0;
sf.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
sf.pFrom = ws.c_str();
sf.pTo = L"C:\\Users\\Me\\Desktop\\folder\0";
SHFileOperation(&sf);
}
// the following is from msdn
// http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/0f749fd8-8a43-4580-b54b-fbf964d68375
wstring s2ws(const string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}