FileSystemEventHandler 的附加参数
我正在尝试编写一个程序,该程序可以监视多个文件夹的文件创建情况,并启动相同的操作,但每个文件夹的设置不同。我的问题是为 FileSystemEventHandler 指定一个额外的参数。我为每个目录创建一个新的 FileWatcher 来监视并添加 Created 操作的处理程序:
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);
}
如何将“mSettings”变量传递给 FileSystemWatcherCreated()?
I'm trying to write a program that could monitor multiple folders for file creations and launch the same action but with different settings for each folder. My problem is in specifying an extra parameter for the FileSystemEventHandler. I create a new FileWatcher for each directory to monitor and add the handler for the 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);
}
How could I get the 'mSettings' variable passed to FileSystemWatcherCreated()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了
FileWatcher
处理程序提供的信息之外,您无法要求更多信息。但是,您可以做的是创建一个小类,该类可以访问配置,并且还可以有一个委托,您可以将其附加到FileWatcher
的Created
事件You can't ask for more information than what the
FileWatcher
handler provides. What you can do however is to create a small classes that have access to the configuration and also have a delegate that you can attach to theFileWatcher
'sCreated
event您需要了解您正在使用什么。
FileSystemEventHandler
的定义是 -public delegate void FileSystemEventHandler(object sender, FileSystemEventArgs e);
您不能传递第三个参数。
为了传递数据“mSettings”,恐怕您可能必须编写自己的额外代码。
You need to understand what you are using.
FileSystemEventHandler
's definition is-public delegate void FileSystemEventHandler(object sender, FileSystemEventArgs e);
You can't pass the third argument.
In order to pass the data 'mSettings', you might have to write your own extra code, I'm afraid.