删除文件,而不是将其标记为删除 [英] Delete a file instead of marking it for deletion

查看:136
本文介绍了删除文件,而不是将其标记为删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Windows(从XP到8.1)编写服务.我需要递归删除文件夹,为此我使用DeleteFileRemoveDirectory. 我不想使用SHFileOperation,因为它有MAX_PATH个限制.

问题在于,有时即使目录为空,RemoveDirectory也会因ERROR_DIR_NOT_EMPTY失败. 我发现这是因为DeleteFile是异步工作的,它仅标记"要删除的文件.因此,在调用RemoveDirectory之前添加一个小的延迟(Sleep)可解决此问题.但我正在寻找一种更清洁的方法.

那么,有没有办法确保标记文件被正确删除呢?

我已经尝试直接在目录上调用FlushFileBuffers,但是没有成功.

有些人声称NtDeleteFile可以删除文件,即使该文件上有打开的句柄.我只是检查了一下,这是错误的,至少在Windows 8.1上是这样:仅当所有句柄都关闭时,文件才被删除.

解决方案

我不想使用SHFileOperation,因为它具有MAX_PATH限制.

在Vista +上,您可以(并且应该)使用 而不是SHFileOperation().这样,您就不受任何路径长度限制,并且可以与SHFileOperation():

不同,它可以在服务中安全地使用.

从服务中调用SHFileOperation是否错误?

从服务中调用SHFileOperation是否错误?修改

因此,您决定从服务中调用SHFileOperation,至少记得禁用复制挂钩

那么,有没有办法确保标记文件被正确删除呢?

并非如此.这些文件正在使用中,或未从高速缓存管理器等中清除. If DeleteFile()成功,您知道文件将最终被删除.您只需要给系统时间实际上删除它.如果收到ERROR_DIR_NOT_EMPTY,请等待几秒钟,然后重试.如果多次尝试后仍然得到ERROR_DIR_NOT_EMPTY,则可能由于文件夹尚未准备好删除而使操作失败,或者只是尝试尝试直到最终成功为止,并定期枚举文件(以防创建新文件). /p>

I am writing a service for Windows (from XP to 8.1). I need to recursively delete a folder and I use DeleteFile and RemoveDirectory for that. I don't want to use SHFileOperation because it has the MAX_PATH limit.

The problem is that sometimes, RemoveDirectory fails with ERROR_DIR_NOT_EMPTY, even if the directory is empty. I found out that this is because DeleteFile works asynchronously, it only "marks" a file for deletion. Therefore, adding a small delay (Sleep) before calling RemoveDirectory fixes the issue. But I am looking for a cleaner method.

So, is there a way to ensure that the marked files are well and truly deleted?

I already tried to call FlushFileBuffers directly on the directory, without success.

Edit: Some are claiming that NtDeleteFile can delete a file even if there are handles open on it. I just checked that and it's wrong, at least on Windows 8.1: the file is removed only when all the handles are closed.

解决方案

I don't want to use SHFileOperation because it has the MAX_PATH limit.

On Vista+, you can (and should) use IFileOperation instead of SHFileOperation(). Then you are not limited to any path length limitations, and it can be used safely in a service, unlike SHFileOperation():

Is it wrong to call SHFileOperation from a service?

Is it wrong to call SHFileOperation from a service? Revised

So you decided to call SHFileOperation from a service, at least remember to disable copy hooks

So, is there a way to ensure that the marked files are well and truly deleted?

Not really. The files are either in use, or not cleared from the cache manager, or the like. If DeleteFile() succeeds, you know the file will be deleted eventually. You just have to give the system time to actually delete it. If you receive ERROR_DIR_NOT_EMPTY, wait a few seconds and try again. If you keep getting ERROR_DIR_NOT_EMPTY after several attempts, either fail your operation since the folder is clearly not ready to be deleted yet, or else just keep trying until it finally succeeds, enumerating files periodically (in case new files are created).

这篇关于删除文件,而不是将其标记为删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆