如何将文件夹删除到回收站

发布于 2024-07-30 13:15:52 字数 657 浏览 5 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-08-06 13:15:53

SHFileOperation 的返回值是一个 int,并且应该指定错误代码。 你得到了什么?

The return value from SHFileOperation is an int, and should specify the error code. What do you get?

伴我老 2024-08-06 13:15:53

我知道这不是正确的方法,但如果您找不到解决方案,您可以尝试这个..

下载文件 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")

梦里的微风 2024-08-06 13:15:53

您已经找到了一个有效的解决方案,但这只是偶然的。 这里的实际问题是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文