2010-04-14 127 views
6

我正在嘗試編寫一個程序,該程序可以監視文件創建的多個文件夾,並針對每個文件夾啓動相同的操作但具有不同的設置。我的問題是在爲FileSystemEventHandler指定一個額外的參數。我創建了一個新的FileWatcher每個目錄監控和添加的處理程序中創建行動:FileSystemEventHandler的其他參數

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?

+0

哪裏'mSettings'被置? – James 2010-04-14 09:35:05

+0

對不起,這個不好的例子。 mSettings被設置在第一個...從當前的配置中,但我認爲它並不真正與問題相關。 – peku 2010-04-14 10:02:41

回答

3
foreach (String config in configs) 
{ 
    ... 
    FileWatcher.Created += (s,e) => DoSomething(e.FullPath, mSettings); 
    ... 
} 
+0

你如何取消訂閱活動? 如果第一個擴展爲mSettings.MyProperty = config,該怎麼辦? – Henrik 2010-04-14 09:53:46

+2

是的,謝謝!完全按照我的想法工作。我知道必須有一個簡單的方法來實現這一點。 – peku 2010-04-14 09:57:08

+1

+1舊帖子,但發現它google'ing它,也幫助了我 – YvesR 2013-06-24 18:27:17

0

你不能要求更多的信息比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); 
     } 
    } 
} 
+1

這就是爲什麼你應該使用閉包,而不是寫所有這些額外的不必要的代碼。 – leppie 2010-04-14 09:42:41

0

你需要了解你使用的是什麼。 FileSystemEventHandler的定義是 -

public delegate void FileSystemEventHandler(object sender, FileSystemEventArgs e);

你不可錯過的第三個參數。 爲了傳遞數據'mSettings',恐怕你可能必須編寫自己的額外代碼。

5

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

也謝謝你,這似乎是我接受的答案更「完整」的版本,但因爲我不需要退訂我認爲我會堅持最簡單的方法。 +1 – peku 2010-04-14 09:58:57