2010-04-14 4 views
6

파일 생성을 위해 여러 폴더를 모니터링하고 각 폴더마다 다른 설정을 사용하여 동일한 작업을 시작할 수있는 프로그램을 작성하려고합니다. 내 문제는 FileSystemEventHandler에 대한 추가 매개 변수를 지정하는 것입니다. 내가 FileSystemWatcherCreated에 전달 된 'mSettings'변수()를 얻을 수있는 방법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); 
} 

을 : 나는 모니터링하고 만든 액션에 대한 처리기를 추가하려면 각 디렉토리에 대한 새로운 FileWatcher을 만들?

+0

경우'mSettings' 설정됩니까? – James

+0

나쁜 예를 들면서 죄송합니다. mSettings는 현재 설정에서 첫 번째로 설정되었지만 질문과 관련이 없다고 생각했습니다. – peku

답변

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

어떻게 이벤트 구독을 취소 하시겠습니까? 첫 번째 ... mSettings.MyProperty = config로 확장하면 어떻게 될까요? – Henrik

+2

네, 고마워요! 내가 원하는만큼 정확하게 작동합니다. 나는 이것을 성취 할 수있는 간단한 방법이 있어야한다는 것을 알고있었습니다. – peku

+1

+1 올드 지위가 있지만 google'ing 그것을 발견하고 도와 줬어. – YvesR

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

0

에 연결할 수 대리자가 작은 클래스를 만드는 것입니다. FileSystemEventHandler의 세 번째 인수를 전달할 수 없습니다 IS-

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

관련 문제