FileSystemEventHandler 的附加参数

发布于 2024-08-29 04:42:53 字数 520 浏览 4 评论 0原文

我正在尝试编写一个程序,该程序可以监视多个文件夹的文件创建情况,并启动相同的操作,但每个文件夹的设置不同。我的问题是为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悟红尘 2024-09-05 04:42:53

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); 
} 

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); 
} 
哭泣的笑容 2024-09-05 04:42:53
foreach (String config in configs)
{
    ...
    FileWatcher.Created += (s,e) => DoSomething(e.FullPath, mSettings);
    ...
}
foreach (String config in configs)
{
    ...
    FileWatcher.Created += (s,e) => DoSomething(e.FullPath, mSettings);
    ...
}
北风几吹夏 2024-09-05 04:42:53

除了 FileWatcher 处理程序提供的信息之外,您无法要求更多信息。但是,您可以做的是创建一个小类,该类可以访问配置,并且还可以有一个委托,您可以将其附加到 FileWatcherCreated 事件

class Program
{
    static void Main(string[] args)
    {
        FileSystemWatcher watcher = new FileSystemWatcher("yourpath");

        var configurations = new IConfiguration[]
                                 {
                                     new IntConfiguration(20),
                                     new StringConfiguration("Something to print")
                                 };

        foreach(var config in configurations)
            watcher.Created += config.HandleCreation;
    }

    private interface IConfiguration
    {
        void HandleCreation(object sender, FileSystemEventArgs e);
    }

    private class IntConfiguration : IConfiguration
    {
        public IntConfiguration(int aSetting)
        {
            ASetting = aSetting;
        }

        private int ASetting { get; set; }

        public void HandleCreation(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("Consume your settings: {0}", ASetting);
        }
    }

     public class StringConfiguration : IConfiguration
    {
        public string AnotherSetting { get; set;}

        public StringConfiguration(string anotherSetting)
        {
            AnotherSetting = anotherSetting;
        }

        public void HandleCreation(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("Consume your string setting: {0}", AnotherSetting);
        }
    }
}

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 the FileWatcher's Created event

class Program
{
    static void Main(string[] args)
    {
        FileSystemWatcher watcher = new FileSystemWatcher("yourpath");

        var configurations = new IConfiguration[]
                                 {
                                     new IntConfiguration(20),
                                     new StringConfiguration("Something to print")
                                 };

        foreach(var config in configurations)
            watcher.Created += config.HandleCreation;
    }

    private interface IConfiguration
    {
        void HandleCreation(object sender, FileSystemEventArgs e);
    }

    private class IntConfiguration : IConfiguration
    {
        public IntConfiguration(int aSetting)
        {
            ASetting = aSetting;
        }

        private int ASetting { get; set; }

        public void HandleCreation(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("Consume your settings: {0}", ASetting);
        }
    }

     public class StringConfiguration : IConfiguration
    {
        public string AnotherSetting { get; set;}

        public StringConfiguration(string anotherSetting)
        {
            AnotherSetting = anotherSetting;
        }

        public void HandleCreation(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("Consume your string setting: {0}", AnotherSetting);
        }
    }
}
心安伴我暖 2024-09-05 04:42:53

您需要了解您正在使用什么。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文