1
這會在桌面上創建一個新文件夾,但它不會將文件夾.pfrom的內容移動到文件夾.pTo。SHFileOperation不會移動文件夾的所有內容
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;
}
手動'push_back('\ 0');'會更簡單。 – Puppy 2011-04-24 10:42:23
if()和while()語句如何轉換爲布爾值? – 2011-04-30 02:56:55
@Geore,我在上面添加了一個解釋。如果還不清楚,請告訴我。 – wimh 2011-04-30 07:22:28