创建同一FileSystemWatcher的多个实例 [英] Create multiple instances of the same FileSystemWatcher
问题描述
我的程序需要监视多个位置,但是每个位置触发相同的代码.由于单个FileSystemWatcher
无法监视多个位置,但是是否可以创建它的多个实例并为每个实例传递文件夹路径?
My program needs to monitor multiple locations, but trigger the same code for each location. As a single FileSystemWatcher
can't monitor multiple locations, but is it possible to create multiple instances of it and pass in a folder path for each?
我无法对每个FileSystemWatcher
进行硬编码,因为需要及时添加越来越多的位置,并且这需要由最终用户完成,因为对我而言,手动对a进行硬编码非常不切实际.每次新的FileSystemWatcher
.所以我的计划是将文件夹路径保存到文件中,该程序只为列表中的每个路径创建一个FileSystemWatcher
.但是我不知道这是否有可能.
I can't hard code each FileSystemWatcher
as more and more locations will need to be added in time and this needs to be done by the end users, as it is highly impractical for me to have to manually hard code a new FileSystemWatcher
each time. So my plan was to have the folder paths saved to a file and the program just creates a FileSystemWatcher
for each path in the list. But I have no idea if this is possible in the slightest.
在这里尝试工厂方法模式"建议: 我收到错误消息:'列表'不包含'添加'的定义
Going on the Factory Method Pattern suggestion here the attempt: I get the errors: "'List' does not contain a definition for 'add'
public void StartWatchers()
{
string[] ArrayPaths = new string[2];
List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
ArrayPaths[0] = @"K:\Daily Record Checker\Test\Test1";
ArrayPaths[1] = @"K:\Daily Record Checker\Test\Test2";
int i = 0;
foreach (String String in ArrayPaths)
{
watcher.add(MyWatcherFatory(ArrayPaths[i]));
i++;
}
//Do other stuff....
//....
//Start my watchers...
foreach (FileSystemWatcher watcher in watchers)
{
Watcher.EnableRaisingEvents = true;
i++;
}
}
FileSystemWatcher MyWatcherFatory(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.Changed += Watcher_Created;
watcher.Path = path;
watcher.Filter = "*.csv";
return watcher;
}
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
System.Threading.Thread.Sleep(1000);
FileInfo fileInfo = new FileInfo(e.FullPath);
if (!IsFileLocked(fileInfo))
{
CheckNumberOfRecordsInFile(e.FullPath);
}
}
推荐答案
使用工厂方法模式.
FileSystemWatcher MyWatcherFatory(string path, object additionalParameters)
{
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.Changed += myWatcherChangedMethod;//Attach them to the same listeners,,,
//Set additional parameters...
return watcher.
}
编辑:根据您进一步提供的信息:
EDIT: Based on the information you further provided:
public void StartWatchers()
{
string[] ArrayPaths = new string[2];
List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
ArrayPaths[0] = @"K:\Daily Record Checker\Test\Test1";
ArrayPaths[1] = @"K:\Daily Record Checker\Test\Test2";
int i = 0;
foreach (String String in ArrayPaths)
{
watcher.add(MyWatcherFatory(ArrayPaths[i]));
i++;
}
//Do other stuff....
//....
//Start my watchers...
foreach (FileSystemWatcher watcherin watchers )
{
watcher.EnableRaisingEvents = true;;
i++;
}
}
FileSystemWatcher MyWatcherFatory(string path)
{
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.Changed += Watcher_Created;
watcher.Path = path;
watcher.Filter = "*.csv";
return watcher;
}
private void Watcher_Created(object sender, FileSystemEventArgs e)
{
System.Threading.Thread.Sleep(1000);
FileInfo fileInfo = new FileInfo(e.FullPath);
if (!IsFileLocked(fileInfo))
{
CheckNumberOfRecordsInFile(e.FullPath);
}
}
这篇关于创建同一FileSystemWatcher的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!