2010-04-14 14 views
6

Sto provando a scrivere un programma in grado di monitorare più cartelle per le creazioni di file e avviare la stessa azione ma con impostazioni diverse per ogni cartella. Il mio problema è nello specificare un parametro aggiuntivo per FileSystemEventHandler. Creo una nuova FileWatcher per ogni directory per monitorare e aggiungere il gestore per l'azione Creato:Parametri aggiuntivi per 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); 
} 

Come avrei potuto ottenere la variabile 'mSettings' passato a FileSystemWatcherCreated()?

+0

dove viene 'mSettings' da impostare? – James

+0

Ci scusiamo per il cattivo esempio. mSettings è impostato nel primo ... dalla configurazione corrente ma ho pensato che non fosse realmente pertinente alla domanda. – peku

risposta

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

Come si annulla l'iscrizione all'evento? Cosa succede se il primo ... si espande in mSettings.MyProperty = config? – Henrik

+2

Sì grazie! Funziona esattamente come volevo. Sapevo che ci doveva essere un modo semplice per raggiungere questo obiettivo. – peku

+1

+1 Vecchio post, ma trovato google'ing e mi ha aiutato, anche – YvesR

0

Non è possibile chiedere più informazioni di quelle fornite dal gestore FileWatcher. Che cosa si può fare però è quello di creare un piccolo classi che hanno accesso alla configurazione e hanno anche un delegato che è possibile allegare a 's il FileWatcherCreated evento

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

Questo è esattamente il motivo per cui è necessario utilizzare chiusure e non scrivere tutto questo codice aggiuntivo non necessario. – leppie

0

È necessario capire che cosa si sta utilizzando. Definizione è-

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

Non è possibile passare il terzo argomento FileSystemEventHandler s'. Per passare i dati 'mSettings', potrei dover scrivere il tuo codice extra, temo.

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

Grazie anche per questo, sembra essere una versione più "completa" della risposta che ho accettato ma dal momento che non avrò bisogno di annullare l'iscrizione penso che rimarrò con l'approccio più semplice. +1 – peku

Problemi correlati