是否可以删除在Windows下的进程打开的文件? [英] Is it possible to delete a file that is opened by a process under windows?
问题描述
为了进行测试和模拟,我想删除目前由我的程序开启的档案。
For testing and simulation purposes I would like to delete a file that is currently open by my process.
CreateFile docs 说明可以在模式下打开文件( FILE_SHARE_DELETE
),它允许打开句柄指向的文件被另一个操作删除。 (我已经尝试并通过CreateFile(FILE_SHARE_DELETE)+ DeleteFile确认了这一点。)
The CreateFile docs state that it is possible to open a file in a mode (FILE_SHARE_DELETE
) that allows the file the open handle is pointing to to be deleted by another operation. (And I already tried and confirmed this via CreateFile(FILE_SHARE_DELETE) + DeleteFile.)
然而,我现在想知道的是,
What I would like to know now however is, whether it is possible at all that a file that is opened by anyone without above mentioned flag to be deleted somehow?
根据我理解的 DeleteFile docs 这不可能使用此功能,如
As far as I understand the DeleteFile docs it would not be possible with this functions, as
如果应用程序尝试删除对于普通I / O打开
或作为内存映射文件的文件,DeleteFile函数将失败。
The DeleteFile function fails if an application attempts to delete a file that is open for normal I/O or as a memory-mapped file.
有没有其他方法来删除已打开但未设置FILE_SHARE_DELETE标志的文件?
Is there any other way to delete a file that is open, but does not have the FILE_SHARE_DELETE flag set?
推荐答案
没有办法这样做。
最近的做法是在重新启动时使用 MoveFileEx ,目标文件名为 NULL
和 MOVEFILE_DELAY_UNTIL_REBOOT在
。 dwFlags
参数中
The closest way to do something like this is to schedule delete on reboot using MoveFileEx with a target filename of NULL
, and MOVEFILE_DELAY_UNTIL_REBOOT
in the dwFlags
parameter.
这篇关于是否可以删除在Windows下的进程打开的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!