如何将文件夹删除到回收站
我在C++、MFC、windows下编程。
我想将一个文件夹删除到回收站中。 我怎样才能做到这一点?
CString filePath = directorytoBeDeletePath;
TCHAR ToBuf[MAX_PATH + 10];
TCHAR FromBuf[MAX_PATH + 10];
ZeroMemory(ToBuf, sizeof(ToBuf));
ZeroMemory(FromBuf, sizeof(FromBuf));
lstrcpy(FromBuf, filePath);
SHFILEOPSTRUCT FileOp;
FileOp.hwnd = NULL
FileOp.wFunc=FO_DELETE;
FileOp.pFrom=FromBuf;
FileOp.pTo = NULL;
FileOp.fFlags=FOF_ALLOWUNDO|FOF_NOCONFIRMATION;
FileOp.hNameMappings=NULL;
bRet=SHFileOperation(&FileOp);
上面的代码有什么问题吗? 它总是失败。
我发现了问题: 文件路径应该是:“c:\abc”而不是“c:\abc\”
I am programing under C++, MFC, windows.
I want to delete a folder into recycle bin.
How can I do this?
CString filePath = directorytoBeDeletePath;
TCHAR ToBuf[MAX_PATH + 10];
TCHAR FromBuf[MAX_PATH + 10];
ZeroMemory(ToBuf, sizeof(ToBuf));
ZeroMemory(FromBuf, sizeof(FromBuf));
lstrcpy(FromBuf, filePath);
SHFILEOPSTRUCT FileOp;
FileOp.hwnd = NULL
FileOp.wFunc=FO_DELETE;
FileOp.pFrom=FromBuf;
FileOp.pTo = NULL;
FileOp.fFlags=FOF_ALLOWUNDO|FOF_NOCONFIRMATION;
FileOp.hNameMappings=NULL;
bRet=SHFileOperation(&FileOp);
Any thing wrong with the code above?
It always failed.
I found the problem:
filePath should be : "c:\abc" not "c:\abc\"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SHFileOperation 的返回值是一个 int,并且应该指定错误代码。 你得到了什么?
The return value from SHFileOperation is an int, and should specify the error code. What do you get?
我知道这不是正确的方法,但如果您找不到解决方案,您可以尝试这个..
下载文件 nircmd.exe 或另一个可以清空回收站的 exe。
然后你通过 system("nircmd.exeemptybin") 调用这些函数
i know it is not the right way but if you cant find a solution you can try this..
download file nircmd.exe or another exe that can empty recycle bin.
then you call these functions by system("nircmd.exe emptybin")
您已经找到了一个有效的解决方案,但这只是偶然的。 这里的实际问题是
pFrom
参数采用特殊格式。 根据 SHFILEOPTS,它存储文件路径列表,每个路径都以 null 结尾,最后一个路径后有一个额外的 null。在您的情况下,这恰好有效,因为
FromBuf
数组比文件名长,并且所有条目都初始化为零。 更通用的解决方案是创建一个对于文件名来说足够长的缓冲区,然后在其后添加两个 nul 字符。 请注意,Windows 文件名可以长于_MAX_PATH
,例如参见https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file#maximum-路径长度限制You have found a solution that works, however it's only by accident. The actual problem here is that the
pFrom
parameter is in a special format. According to the MSDN docs for SHFILEOPTS, it stores a list of file paths, each one null-terminated, and an extra null after the last one.In your case this happens to work because the
FromBuf
array is longer than the filename and all the entries are initialised to zero. The more general solution is to create a buffer that is long enough for the filename and then add two nul characters after it. Note that Windows filenames can be longer than_MAX_PATH
, eg see https://learn.microsoft.com/en-us/windows/desktop/fileio/naming-a-file#maximum-path-length-limitation