ReadDirectoryChangesW 阻止删除监视目录 [英] ReadDirectoryChangesW blocks deleting the watched directory

查看:42
本文介绍了ReadDirectoryChangesW 阻止删除监视目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ReadDirectoryChangesW API 使用 python 在 Windows 上观察创建/删除/重命名更改的目录.这是我的代码,它运行良好:

I am trying to watch a directory for create/delete/rename changes on windows with python using the ReadDirectoryChangesW API. This is my code and it is working fine:

results = win32file.ReadDirectoryChangesW(self.hDir, 8192, True, self.type, None,
                                           None)
for action, file in results:
    full_filename = os.path.join (self.source_path, file)
    if   action == 1:                                    # Created
        self.fileCreated(full_filename)
    elif action == 2:                                    # Deleted
        self.fileDeleted(full_filename)
    elif action == 3:                                    # Updated
        self.fileUpdated(full_filename)
    elif action == 4:                                    # Renamed from something
        renamed_file = full_filename
    elif action == 5:                                    # Renamed to something
        self.fileRenamed(renamed_file, full_filename)

但是,当我尝试从 python 或 Windows 资源管理器中删除监视文件夹时,我得到:

However, when I try to delete the watched folder from python or from windows explorer, I get:

WindowsError: [Error 32] 该进程无法访问该文件,因为它正被另一个进程使用:'c:\users\user\appdata\local\temp\new_dir'

我相信这是有道理的,但我应该如何解决这个问题?因为我的应用程序应该允许用户删除一个被监视的文件夹.我尝试了异步方法的解决方案 http://www.themacaque.com/?p=859,但没有帮助.

I believe this makes sense, but how should I solve this? Because my application should allow the user to remove a watched folder. I tried the solution for the asynchronous method http://www.themacaque.com/?p=859, but it didn't help.

提前致谢!

推荐答案

来自 this博文:

[ReadDirectoryChangesW] 的另一个潜在缺陷是引用的目录本身现在正在使用",因此无法删除.要监视目录中的文件并仍允许删除该目录,您必须监视父目录及其子目录.

Another potential pitfall of [ReadDirectoryChangesW] is that the referenced directory itself is now "in use" and so can't be deleted. To monitor files in a directory and still allow the directory to be deleted, you would have to monitor the parent directory and its children.

该帖子还提供了有关正确使用 ReadDirectoryChangesW 的更多详细信息

The post also provides some more details on proper use of ReadDirectoryChangesW

这篇关于ReadDirectoryChangesW 阻止删除监视目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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