wstring path = L"C:\\Users\\oneworduser\\Desktop\\trash";
LPCWSTR origin = (path + L"\\" + files.at(i)).wstring::c_str();
LPCWSTR destination = (path + L"\\" + extensions.at(i) + L"\\" + files.at(i)).wstring::c_str();
//move file
BOOL b = MoveFileW(origin, destination);
MoveFileW 返回 false。
files.at(i) 是当前文件的 wstring
名称。
extensions.at(i) 是在 .在 files.at(i) 中。例如:
如果 files.at(0)
是 mytext.txt
,则 extensions.at(0)
是 txt
。
MoveFileW 返回 false,如果我 GetLastError()
,我会收到错误 123,即 ERROR_INVALID_NAME
。
为什么我不能移动文件?
最佳答案
你有未定义的行为。 std::wstring::operator+
正在返回一个临时值,origin
和 destination
最终指向释放的内存。如果您在调试器中查看过您的程序,您几乎肯定会看到这一点。
将您的代码更改为:
wstring path = L"C:\\Users\\oneworduser\\Desktop\\trash";
wstring origin = path + L"\\" + files.at(i);
wstring destination = path + L"\\" + extensions.at(i) + L"\\" + files.at(i);
//move file
BOOL b = MoveFileW(origin.c_str(), destination.c_str());
关于c++ - 尝试使用带有 C++ 的 MoveFile 移动文件时出现 ERROR_INVALID_NAME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38471674/