简体   繁体   English

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

[英]How to remove the file which has opened handles?

PROBLEM HISTORY:问题历史:
Now I use Windows Media Player SDK 9 to play AVI files in my desktop application.现在我使用Windows Media Player SDK 9在我的桌面应用程序中播放 AVI 文件。 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 .它在 Windows XP 上运行良好,但是当我尝试在 Windows 7 上运行它时我发现了一个错误 -我无法在播放后立即删除 AVI 文件 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.在 Windows XP 上,我在播放文件期间有 2 个打开的文件句柄,它们在关闭播放窗口后关闭,但在 Windows 7 上,我在播放文件期间已经打开了 4 个句柄,其中 2 个在播放窗口关闭后保留。 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"?可能存在“强制删除”之类的东西?

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:这是我编写的一个函数,用于在 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;
}

Technically you can delete a locked file by using MoveFileEx and passing in MOVEFILE_DELAY_UNTIL_REBOOT .从技术上讲,您可以通过使用MoveFileEx并传入MOVEFILE_DELAY_UNTIL_REBOOT来删除锁定的文件。 When the lpNewFileName parameter is NULL, the Move turns into a delete and can delete a locked file. lpNewFileName参数为NULL时,Move变为delete,可以删除锁定的文件。 However, this is intended for installers and, among other issues, requires administrator privileges.但是,这是为安装人员准备的,除其他问题外,还需要管理员权限。

Have you checked which application is still using the avi file?您是否检查过哪个应用程序仍在使用 avi 文件? you can do this by using handle.exe .你可以通过使用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).另一种解决方案是使用unlocker应用程序(免费)。

One of the above two method should fix your problem.上述两种方法之一应该可以解决您的问题。

Have you already tried to ask WMP to release the handles instead?您是否已经尝试要求 WMP 释放句柄? ( IWMPCore::close seems to do that) IWMPCore::close似乎这样做)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM