SHFileOperation 不会移动文件夹的所有内容
这会在桌面上创建一个新文件夹,但不会将文件夹 .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;
}
this makes a new folder on the desktop, but it doesn't move the contents of the folder .pfrom to the folder .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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SHFileOperation 需要一个以双 null 结尾的字符串。但您不能为此使用 std::string 或 std::wstring 。另请参阅双 null 终止字符串。
当您这样做时:
字符串的 + 运算符不会看到第二个 null 终止符,因为它在第一个 null 处停止。
这里有一个方法可以解决这个问题:
if() 和 while() 语句如何转换为布尔值?
例如这个 if 语句:
也可以写成:
or:
我组合了赋值TCHAR* 和单个语句中的检查。当指针转换为布尔值时,NULL 变为 false,所有其他值变为 true。
SHFileOperation requires a double null terminated string. But you can't use std::string or std::wstring for that. See also Double null-terminated string.
When you do:
The + operator of the string does not see the second null-termination, because it stops at the first null.
Here is a way you can solve this:
how are the if() and while() statements converted to a boolean?
For example this if statement:
Can also be written like:
or:
I combined the assignment of the TCHAR* and the check in a single statement. When a pointer is converted to a boolean, then NULL becomes false, and all other values become true.