pek*_*eku 7 c# filesystemwatcher
我正在尝试编写一个程序,可以监视多个文件夹以进行文件创建,并启动相同的操作,但每个文件夹的设置不同.我的问题是为FileSystemEventHandler指定一个额外的参数.我为每个目录创建一个新的FileWatcher来监视并添加Created-action的处理程序:
foreach (String config in configs)
{
...
FileWatcher.Created += new System.IO.FileSystemEventHandler(FileSystemWatcherCreated)
...
}
void FileSystemWatcherCreated(object sender, System.IO.FileSystemEventArgs e, MySettings mSettings)
{
DoSomething(e.FullPath, mSettings);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能将'mSettings'变量传递给FileSystemWatcherCreated()?
foreach (String config in configs)
{
...
FileWatcher.Created += (s,e) => DoSomething(e.FullPath, mSettings);
...
}
Run Code Online (Sandbox Code Playgroud)
foreach (String config in configs)
{
...
MySettings mSettings = new MySettings(...); // create a new instance, don't modify an existing one
var handler = new System.IO.FileSystemEventHandler( (s,e) => FileSystemWatcherCreated(s,e,msettings) );
FileWatcher.Created += handler;
// store handler somewhere, so you can later unsubscribe
...
}
void FileSystemWatcherCreated(object sender, System.IO.FileSystemEventArgs e, MySettings mSettings)
{
DoSomething(e.FullPath, mSettings);
}
Run Code Online (Sandbox Code Playgroud)