使用 FileSystemWatcher 监视多个文件夹
在 C# 中使用 FileSystemWatcher 监视多个文件夹(不是子目录)的最佳方法是什么?
Whats the best way to monitor multiple folders (not subdirectories) using FileSystemWatcher in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为 FSW 不支持监视多个文件夹,因此只需为要监视的每个文件夹实例化一个即可。不过,您可以将事件处理程序指向相同的方法,这最终应该像我认为您想要的那样工作。
I don't think FSW supports monitoring multiple folders, so just instantiate one per folder you want to monitor. You can point the event handlers at the same methods, though, which should end up working like I think you want.
最简单的方法是创建 FileSystemWatcher 的多个实例目的。
http://www.c-sharpcorner.com/UploadFile/mokhtarb2005/FSWatcherMB12052005063103AM /FSWatcherMB.aspx
您必须确保正确处理两个文件夹之间的事件:
FileSystemEventArgs
The easiest way is to create multiple instances of the FileSystemWatcher object.
http://www.c-sharpcorner.com/UploadFile/mokhtarb2005/FSWatcherMB12052005063103AM/FSWatcherMB.aspx
You'll have to make sure the you handle events between the two folders correctly:
Link to FileSystemEventArgs
FileSystemWatcher 开箱即用,仅支持监视单个父目录。要监视多个同级目录,您需要创建 FileSystemWatcher 的多个实例。
不过,您可以尝试利用 FileSystemWatcher 包含子目录的功能来欺骗此行为。您可以创建一个 NTFS 连接点(也称为符号链接)作为您正在观看的目录的子目录。 Sysinternals 出名的 Mark Russinovich 有一个名为 Junction 的实用程序,用于简化创建和管理符号链接。
请注意,您只能创建指向本地计算机上的目录的符号链接。
Out of the box, FileSystemWatcher only supports monitoring a single parent directory. To monitor multiple sibling directories, you would need to create multiple instances of FileSystemWatcher.
You can try cheating this behavior, however, by taking advantage of FileSystemWatcher's ability to include subdirectories. You can create an NTFS junction point (aka symbolic link) as a subdirectory from the directory you are watching. Mark Russinovich of Sysinternals fame has a utility called Junction to simplify creation and management of symlinks.
Note that you can only create symlinks to directories on your local machine.
虽然这是一个老问题,但我决定回答,因为我在任何地方都找不到好的答案。
那么,目的是使用 FileSystemWatcher 监视多个文件夹(而不是子目录)?这是我的建议:
Although this is an old question I decided to answer, because I couldn't find a good answer anywhere.
So, the aim was to monitor multiple folders (not subdirectories) using FileSystemWatcher? Here's my suggestion:
您是否可以简单地使用 FileSystemWatcher 的多个实例,每个目录一个?
Can you simply use multiple instances of the FileSystemWatcher, one for each directory?
您必须实例化 FileSystemWatcher 对象的多个实例。不过,您可以将事件绑定到相同的方法,并使用发送者对象来确定哪个
FileSystemWatcher
触发了该事件。You would have to instantiate multiple instances of the
FileSystemWatcher
object. Though you can bind the Events to the same method and use the sender object to determine whichFileSystemWatcher
triggered the event.或者您可以在代码中传递路径,以标记监视的域的特定范围,如下所示:
多个监视器链接
希望这有帮助。
or you can pass the paths in-code, to mark a certain range of the domain watched like in this :
multiple monitor link
hope this helps.