2011-12-27 3 views
1

동기화 된 드롭 상자 폴더에 항목이 추가 된 후이를 (전체 동기화를 허용하기 위해) 기다렸다가 항목을 이동할 때 감지하는 작은 유틸리티 서비스를 작성하려고합니다. s)를 추가 처리를위한 날짜 스탬프 스테이징 폴더에 저장합니다. 충분히 간단한 ... 여기 보관함이있는 파일 감시자 클래스

내 코드입니다 : 하나 개의 항목이 동기화 된 디렉토리에 추가되는 경우

static void Main(string[] args) 
    { 
     var watcher = new FileSystemWatcher(); 

     string _path = @"E:\IMPORT\Dropbox\"; 
     watcher.Path = _path; 
     watcher.EnableRaisingEvents = true; 
     watcher.Created += new FileSystemEventHandler(watcher_Created); 


     Console 
     .WriteLine("FileSystemWatcher ready and listening to changes in :\n\n" 
        + _path); 

     watcher.Path = _path; 
     Console.ReadLine(); 
    } 

    static void watcher_Created(object sender, FileSystemEventArgs e) 
    { 
     Thread.Sleep(3000); 
     Console.WriteLine(e.Name + " file has been created."); 

     string filename = Path.GetFileName(e.FullPath); 
     string path = @"E:\IMPORT\Staging\" 
         + DateTime.Now.ToFileTime().ToString() 
         + @"\"; 

     try 
     { 
      Directory.CreateDirectory(path); 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine("Error: " + ex.ToString()); 
     } 
     try 
     { 
      File.Move(e.FullPath, path + filename); 
     } 
     catch(Exception ex) 
     { 
      Console.WriteLine("Error: " + ex.ToString()); 
     } 
    } 

이 코드는하지만 여러 항목이 추가됩니다 잘 작동 것 및 지연이있을 필요가있다 보관함에 항목을 추가 할 수 있습니다. 내가 이것을 어떻게 성취 할 수 있을지에 대한 아이디어가 있습니까?

+0

지연 이유는 무엇입니까? 준비 폴더가 다시 사용되지 않도록하려면? –

+1

@Austin, 20MB .pdf가 전송되면 드롭 박스 디렉토리에서 완전히 동기화 할 시간이 있습니다. –

답변

4

여기서 가장 좋은 방법은 FileSystemWatcher를 제거하고 주기적으로 디렉터리 모니터링 (예 : BackgroundWorker 스레드 또는 Timer-triggered 이벤트의 루프)으로 바꾸는 것입니다.

이 디자인에서는 파일의 타임 스탬프를 현재 시간과 비교하고 충분한 시간이 경과했다고 생각할 때 파일을 처리하는 방식으로 파일 처리를 지연시킬 수 있습니다.

이 디자인은 FileSystemWatcher가 접근하지 않는 디렉토리에 이미있는 파일로 응용 프로그램을 다시 시작하는 것을 지원합니다.