如何删除已打开句柄的文件?
问题历史记录:
现在,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您并不是唯一一个可以处理您的文件的人。其他进程和服务也可以打开该文件。因此,在他们释放句柄之前不可能删除它。您可以在这些句柄打开时重命名该文件。您可以在这些句柄打开时复制文件。但是,不确定是否可以将文件移动到另一个容器?
其他流程&服务,特别是包括杀毒、索引等。
下面是我写的一个在Windows下实现“立即删除”的函数:
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:
从技术上讲,您可以使用
MoveFileEx
并传入MOVEFILE_DELAY_UNTIL_REBOOT
。当lpNewFileName
参数为NULL时,Move变为删除,可以删除锁定的文件。然而,这是针对安装人员的,并且除其他问题外,还需要管理员权限。Technically you can delete a locked file by using
MoveFileEx
and passing inMOVEFILE_DELAY_UNTIL_REBOOT
. When thelpNewFileName
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.您是否检查过哪个应用程序仍在使用该 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.
您是否已尝试要求 WMP 释放手柄? (IWMPCore::close 似乎这样做)
Have you already tried to ask WMP to release the handles instead? (IWMPCore::close seems to do that)