如何删除已打开句柄的文件?

发布于 2024-09-24 19:51:03 字数 440 浏览 6 评论 0原文

问题历史记录:
现在,我使用 Windows Media Player SDK 9 在桌面应用程序中播放 AVI 文件。它在 Windows XP 上运行良好,但当我尝试在 Windows 7 上运行它时,我遇到了一个错误 - 我无法在播放后立即删除 AVI 文件。问题是存在打开的文件句柄。在 Windows XP 上,我在播放文件期间有 2 个打开的文件句柄,它们在播放窗口关闭后关闭,但在 Windows 7 上,我在播放文件期间已经有 4 个打开的句柄,其中 2 个在播放窗口关闭后保留。只有在关闭应用程序后它们才会变得免费。

问题:
我该如何解决这个问题? 如何删除已打开句柄的文件? 可能存在“强制删除”之类的东西?

PROBLEM HISTORY:
Now I use Windows Media Player SDK 9 to play AVI files in my desktop application. It works well on Windows XP but when I try to run it on Windows 7 I caught an error - I can not remove AVI file immediately after playback. The problem is that there are opened file handles exist. On Windows XP I have 2 opened file handles during the playing file and they are closed after closing of playback window but on Windows 7 I have already 4 opened handles during the playing file and 2 of them remain after the closing of playback window. They are become free only after closing the application.

QUESTION:
How can I solve this problem? How to remove the file which has opened handles? May be exists something like "force deletion"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

清旖 2024-10-01 19:51:03

问题是您并不是唯一一个可以处理您的文件的人。其他进程和服务也可以打开该文件。因此,在他们释放句柄之前不可能删除它。您可以在这些句柄打开时重命名该文件。您可以在这些句柄打开时复制文件。但是,不确定是否可以将文件移动到另一个容器?

其他流程&服务,特别是包括杀毒、索引等。

下面是我写的一个在Windows下实现“立即删除”的函数:

bool DeleteFileNow(const wchar_t * filename)
{
    // don't do anything if the file doesn't exist!
    if (!PathFileExistsW(filename))
        return false;

    // determine the path in which to store the temp filename
    wchar_t path[MAX_PATH];
    wcscpy_s(path, filename);
    PathRemoveFileSpecW(path);

    // generate a guaranteed to be unique temporary filename to house the pending delete
    wchar_t tempname[MAX_PATH];
    if (!GetTempFileNameW(path, L".xX", 0, tempname))
        return false;

    // move the real file to the dummy filename
    if (!MoveFileExW(filename, tempname, MOVEFILE_REPLACE_EXISTING))
    {
        // clean up the temp file
        DeleteFileW(tempname);
        return false;
    }

    // queue the deletion (the OS will delete it when all handles (ours or other processes) close)
    return DeleteFileW(tempname) != FALSE;
}

The problem is that you're not the only one getting handles to your file. Other processes and services are also able to open the file. So deleting it isn't possible until they release their handles. You can rename the file while those handles are open. You can copy the file while those handles are open. Not sure if you can move the file to another container, however?

Other processes & services esp. including antivirus, indexing, etc.

Here's a function I wrote to accomplish "Immediate Delete" under Windows:

bool DeleteFileNow(const wchar_t * filename)
{
    // don't do anything if the file doesn't exist!
    if (!PathFileExistsW(filename))
        return false;

    // determine the path in which to store the temp filename
    wchar_t path[MAX_PATH];
    wcscpy_s(path, filename);
    PathRemoveFileSpecW(path);

    // generate a guaranteed to be unique temporary filename to house the pending delete
    wchar_t tempname[MAX_PATH];
    if (!GetTempFileNameW(path, L".xX", 0, tempname))
        return false;

    // move the real file to the dummy filename
    if (!MoveFileExW(filename, tempname, MOVEFILE_REPLACE_EXISTING))
    {
        // clean up the temp file
        DeleteFileW(tempname);
        return false;
    }

    // queue the deletion (the OS will delete it when all handles (ours or other processes) close)
    return DeleteFileW(tempname) != FALSE;
}
血之狂魔 2024-10-01 19:51:03

从技术上讲,您可以使用 MoveFileEx 并传入 MOVEFILE_DELAY_UNTIL_REBOOT。当lpNewFileName参数为NULL时,Move变为删除,可以删除锁定的文件。然而,这是针对安装人员的,并且除其他问题外,还需要管理员权限。

Technically you can delete a locked file by using MoveFileEx and passing in MOVEFILE_DELAY_UNTIL_REBOOT. When the lpNewFileName parameter is NULL, the Move turns into a delete and can delete a locked file. However, this is intended for installers and, among other issues, requires administrator privileges.

甜嗑 2024-10-01 19:51:03

您是否检查过哪个应用程序仍在使用该 avi 文件?
您可以使用 handle.exe 来执行此操作。您可以在关闭正在使用该文件的进程后尝试删除/移动该文件。

另一种解决方案是使用解锁应用程序(免费)。

上述两种方法之一应该可以解决您的问题。

Have you checked which application is still using the avi file?
you can do this by using handle.exe. You can try deleting/moving the file after closing the process(es) that is/are using that file.

The alternative solution would be to use unlocker appliation (its free).

One of the above two method should fix your problem.

早乙女 2024-10-01 19:51:03

您是否已尝试要求 WMP 释放手柄? (IWMPCore::close 似乎这样做)

Have you already tried to ask WMP to release the handles instead? (IWMPCore::close seems to do that)

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