Mono下的FileSystemWatcher-监视子目录 [英] FileSystemWatcher under mono - watching subdirs

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

问题描述

我有问题.我在FileSystemWatcher上写了一个包装程序,用于检测 root 文件夹及其所有子文件夹中的更改.没什么好看的:

I have a problem. I have written a wrapper over FileSystemWatcher that detects changes in root folder and all of its subfolders. Nothing fancy:

FileSystemWatcher watcher = new FileSystemWatcher ();
watcher.Path = this.Root;
watcher.IncludeSubdirectories = true;
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.LastAccess | NotifyFilters.DirectoryName | NotifyFilters.FileName;
watcher.Changed += new FileSystemEventHandler (watcher_Changed);
watcher.Deleted += new FileSystemEventHandler (watcher_Deleted);
watcher.Created += new FileSystemEventHandler (watcher_Created);
watcher.Renamed += new RenamedEventHandler (watcher_Renamed);
watcher.EnableRaisingEvents = true;

在Windows下的.NET中,它的工作原理像一个超级按钮.但是,当我将代码移植到mono并在OSX下运行代码时,它只能在根文件夹中正常工作.

While in .NET, under Windows, it works like a charm. But when I ported the code to mono and ran the code under OSX, it works properly only in the root folder.

我现在已经注意到的问题:

Issues I have noticed by now:

  • 观察程序启动时,根目录下已经存在的文件夹中的操作不会引发事件

  • Events are not raised for operations within folders already existing under root at the time the watcher starts

通过EventArgs.FullPath属性获得的路径不正确(当我将文件复制到path_to_root/some/more/subdirs/some.file时,获得的路径只是path_to_root/some.file). /p>

Paths I get via EventArgs.FullPath property are not correct (when I copy a file to path_to_root/some/more/subdirs/some.file, the path I get is just path_to_root/some.file).

路径不正确的问题已经在一年前报告过(看起来已经解决了),但是我的单声道来自去年12月(MonoDevelop在参考"部分中说它是4.0.0.0版,这就是我所能说的)有关发行版)和错误仍然存​​在... 看: https://bugzilla.xamarin.com/show_bug.cgi?id=5747

The issue with unproper paths has been already reported one year ago (and looks like it was solved) but my mono comes from December last year (MonoDevelop says in References section it is version 4.0.0.0, it is all I can say about the distribution) and the bugs are still there... See: https://bugzilla.xamarin.com/show_bug.cgi?id=5747

有什么想法吗?我真的很好奇是否有一种解决方法,它不需要编写自己的观察程序来反复轮询文件系统,也不需要为 root ...

Any ideas? I am really curious if there is a workaround not requiring writing own watcher that polls the file system repeatedly or starting separate watcher for every folder under root...

提前谢谢!

推荐答案

据我所知,这在OS X上的Mono中根本不起作用.我上周遇到了它,找不到任何错误报告,所以我在这里报告了它: https://bugzilla.xamarin.com/show_bug.cgi?id=16259

As far as I can tell, this simply does not work in Mono on OS X. I encountered it last week and could not find any bug report for it, so I reported it here: https://bugzilla.xamarin.com/show_bug.cgi?id=16259

据我可以遵循 KEventWatcher的实现,则在创建观察程序时不执行任何操作来订阅子目录.我认为它唯一订阅子目录的时间是当它检测到它们被添加到PostEvent中时.即使它确实订阅了创建时的所有子目录,也可能不是一个很好的解决方案.底层的kevent机制将需要为每个子目录打开一个文件描述符,最终可能会导致大量的文件描述符.

As far as I can follow the implementation of KEventWatcher, it doesn't do anything to subscribe to subdirectories when the watcher is created. I think the only time it subscribes to subdirectories is when it detects them being added in PostEvent. Even if it did subscribe to all subdirectories on creation, it might not be a great solution. The underlying kevent mechanism would require an open file descriptor for every subdirectory, which could end up being an awful lot of file descriptors.

Mono确实有FileSystemWatcher的其他实现,但是我相信实现的选择在编译时已嵌入到Mono运行时中.慢速且效率低下的默认观察程序可通过仅每秒大约扫描整个目录树的方式在所有平台上运行,但只有在没有特定于平台的实现可用时才选择它.

Mono does have other implementations of the FileSystemWatcher, but I believe the selection of implementation is baked into the Mono runtime when it's compiled. There is a slow and inefficient default watcher that works on all platforms by simply scanning the entire directory tree every second or so, but it is only selected if there isn't a platform-specific implementation available.

恐怕要说,看来最好的选择是您建议的解决方法之一-手动扫描更改或为每个目录创建FileSystemWatcher.

I'm afraid to say, it looks like your best bet are either of the workarounds you suggest - scanning for changes manually or creating a FileSystemWatcher for every directory.

这篇关于Mono下的FileSystemWatcher-监视子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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