多个可配置FileSystemWatcher的方法 [英] Multiple Configurable FileSystemWatcher methods
问题描述
在我创建了监控一个目录所在的路径是可配置的通过引用一个配置文件,这样的Windows服务在过去的:
In the past I have created Windows Services which monitor one directory where the path is configurable by referencing a config file like this:
fileSystemWatcher1.Path = ConfigurationManager.AppSettings["WatchPath1"];
我也通过定义多个FileSystemWatcher的方法观看多个配置的路径。
I've also watched multiple configurable paths by defining multiple fileSystemWatcher methods.
fileSystemWatcher1.Path = ConfigurationManager.AppSettings["WatchPath1"];
fileSystemWatcher2.Path = ConfigurationManager.AppSettings["WatchPath2"];
fileSystemWatcher3.Path = ConfigurationManager.AppSettings["WatchPath3"];
以上的作品,如果我的前手知道我有可能会多少文件夹,监视,所以我的问题是, ,我可以采取什么办法,使这个充满活力的时候,我不知道有多少文件夹需要进行监测?我想要做的就是继续使用配置或XML文件,并为每个条目的文件中指定的路径中创建FileSystemWatcher的。
The above works if I know before hand how many folders I am likely going to monitor so my question is, what approach can I take to make this dynamic when I don't know how many folders need to be monitored? What I'd like to do is continue to use a config or XML file and for each entry create FileSystemWatcher with the path specified in the file.
我还需要能够动态地创建为每个FileSystemWatcher的所述的方法,当被触发的文件系统事件可以采取这样的具体行动。
I would also need to be able to dynamically create the a method for each FileSystemWatcher so specific actions can be taken when a file system event is triggered.
示例代码动态创建的:
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
Library.WriteErrorLog("New file detected in watch folder. File: " + e.FullPath);
// Do stuff with file
}
这是可能的东西吗?如果是这样,我怎么能去实现这一目标?
Is this possible? If so how could I go about achieving this?
推荐答案
当您启动类存储FileSystemWatcher的对象列表,你初始化
Store a list of FileSystemWatcher object, which you initialise when you start the class.
List<FileSystemWatcher> fsWatchers = new List<FileSystemWatcher>();
要添加新的观察者...
To add a new watcher...
public void AddWatcher(String wPath) {
FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = wPath;
fsw.Created += file_OnCreated;
fsw.Changed += file_OnChanged;
fsw.Deleted += file_OnDeleted;
fsWatchers.Add(fsw);
}
private void file_OnDeleted(object sender, FileSystemEventArgs e) {
}
private void file_OnChanged(object sender, FileSystemEventArgs e) {
}
private void file_OnCreated(object sender, FileSystemEventArgs e) {
}
在每个事件处理程序,投发件人一个FileSystemWatcher的,如果你需要与它直接交互。
要获得完整的路径,使用的事件参数的get方法对象(E)。
In each of the event handlers, cast sender to a FileSystemWatcher if you need to interact directly with it. To get the full path, use the get methods on the event args object (e).
您可能会通过使用单个事件简化了一点处理程序分配给在FileSystemWatcher的所有事件。
You could potentially simplify it a little by having a single event handler assigned to all the events on the FileSystemWatcher.
private void file_OnFileEvent(object sender, FileSystemEventArgs e) {
String path = e.FullPath;
if (e.ChangeType == WatcherChangeTypes.Changed) {
} else if (e.ChangeType == WatcherChangeTypes.Created) {
}
}
这篇关于多个可配置FileSystemWatcher的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!