2014-05-22 2 views
2

내 FileSystemWatcher가 이벤트를 발생시키지 않습니다. 나는이 유사한 질문을 보았다. 아무도 내 문제에 대한 대답이 아닌 것 같다.FileSystemWatcher 이벤트가 실행되지 않음

* 편집 : 내 목표는 XLS 파일을 디렉토리에 복사하거나 디렉토리에 만들 때를 포착하는 것이다.

모니터 클래스 : 목록 상자와

public class Monitor 
{ 
    FileSystemWatcher watcher = new FileSystemWatcher(); 
    readonly string bookedPath = @"\\SomeServer\SomeFolder\"; 

    public delegate void FileDroppedEvent(string FullPath); 
    public event FileDroppedEvent FileDropped; 

    public delegate void ErrorEvent(Exception ex); 
    public event ErrorEvent Error; 

    public Monitor() 
    { 
     watcher.Path = bookedPath; 
     watcher.Filter = "*.xls"; 
     watcher.NotifyFilter = NotifyFilters.LastWrite; 
     watcher.Changed += new FileSystemEventHandler(watcher_Changed); 
     watcher.Error += new ErrorEventHandler(watcher_Error); 
    } 

    void watcher_Error(object sender, ErrorEventArgs e) 
    { 
     Error(e.GetException()); 
    } 

    void watcher_Changed(object sender, FileSystemEventArgs e) 
    { 
     if (e.ChangeType != WatcherChangeTypes.Created) return; 
     FileDropped(e.FullPath); 
    } 

    public void Start() 
    { 
     watcher.EnableRaisingEvents = true; 
    } 

    public void Stop() 
    { 
     watcher.EnableRaisingEvents = false; 
    } 
} 

간단한 형태 :

public partial class Form1 : Form 
{ 
    Monitor monitor = new Monitor(); 

    public Form1() 
    { 
     InitializeComponent(); 
     FormClosing += new FormClosingEventHandler(Form1_FormClosing); 
     Load += new EventHandler(Form1_Load); 
     monitor.FileDropped += new Monitor.FileDroppedEvent(monitor_FileDropped); 
     monitor.Error += new Monitor.ErrorEvent(monitor_Error); 
    } 

    void Form1_Load(object sender, EventArgs e) 
    { 
     monitor.Start(); 
    } 

    void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     monitor.Stop(); 
    } 

    void monitor_Error(Exception ex) 
    { 
     listBox1.Items.Add(ex.Message); 
    } 

    void monitor_FileDropped(string FullPath) 
    { 
     listBox1.Items.Add(FullPath); 
    } 
} 

내가 뭘 잘못하고 있니?

+0

코드가 실행중인 사용자가 네트워크 경로에 액세스 할 수 있습니까? – adrianbanks

+0

예. 실행 중이며 액세스 권한이 있습니다 – DontFretBrett

+0

다음 스레드가 유용 할 수 있습니다. http://stackoverflow.com/questions/11219373/filesystemwatcher-to-watch-unc-path – Edin

답변

-1

귀하의 문제는 내가 생각하는 필터와 이벤트에 있습니다. NotifyFilters.LastAccess은 파일을 열 때만 트리거됩니다. 사용해보기 :

NotifyFilters.LastWrite | NotifyFilters.CreationTime 

작성/작성된 파일을 봅니다. 다음, 새로 생성 된 파일을 처리 할 수 ​​Created 대리인에게 훅 :

watcher.Created += YourDelegateToHandleCreatedFiles 

방법 FileSystemWatcher 작품을 먼저 이벤트가 트리거 제한 할 NotifyFilters을 사용하는 것입니다. 그런 다음 실제 이벤트를 사용하여 작업을 수행합니다. Created 이벤트에 연결하면 파일을 만들 때만 작업을 수행 할 수 있습니다.

+0

이 방법을 사용해 보겠다.하지만 실제로는 새 파일이 만들어 지거나 디렉토리에 복사 될 때만 캡처하고 싶다. – DontFretBrett

+0

그럼'NotifyFilters.LastWrite | NotifyFilters.CreationTime' – Haney

+0

귀하의 제안을 시도했지만 여전히 실행되지 않았습니다 :/ – DontFretBrett

2

시험해보세요. 매우 비슷한 작업을 위해 나를 위해 일한다.

watcher.NotifyFilter = NotifyFilters.FileName; 
watcher.Created += new FileSystemEventHandler(handler);  
watcher.Renamed += new RenamedEventHandler(handler); 
+0

시도해 보니 파일을 디렉토리에 복사했습니다. 해고 된 건 아무것도 없었어. watcher_change 이벤트의 경우에도 중단 점을 설정하므로 내 사용자 지정 이벤트에 문제가 없습니다. 그래도 고마워 – DontFretBrett

0

파일 메타 데이터가 아직 업데이트되지 않았기 때문일 수 있습니다. 이 문제는 파일을 계속 쓰는 중 발생할 수 있습니다.