2012-12-18 17 views
0

我需要觀察一個文件夾(在網絡共享上),並在文件名發生更改並將文件移動到子目錄(全部一個事件)時收到通知。這個事件可能每天發生兩次。我將有多個FileSystemWatchers爲同一件事觀看多個文件夾。FileSystemWatcher - 我會得到緩衝區溢出還是類似的?

但是,FileSystemWatcher是臭名昭着的不良事件,我不能有這種情況發生。我已經嘗試複製環境,它似乎工作,但我不知道是否因爲我正在做某些事情。

如果我只看到OnRenamed事件,我是否仍然有問題或可以確定我不會錯過活動?

+2

您的問題與其標題有什麼關係?我沒有看到有關緩衝區溢出的任何信息。 –

+0

使用默認緩衝區大小,絕對最差的情況是它可以存儲15個事件。更典型的是大約30或40,這取決於文件路徑名的長度。這當然不會對每天發生兩次的重命名事件進行測試。 –

+0

@Hans Passant是否所有'FileSystemWatcher'都使用相同的緩衝區? – Cheetah

回答

0

在一個正常工作的網絡中,您不應該有任何與丟失事件相關的錯誤,但您應該意識到網絡上的簡單呃逆會導致您的FileSystemWatcher監控無效。

與網絡共享連接的瞬間下降將觸發FileSystemWatcher中的錯誤,即使連接將重新建立,FileSystemWatcher也不會再收到任何通知。

這是MSDN

static void Main() 
{ 
    // Create a new FileSystemWatcher and set its properties. 
    FileSystemWatcher watcher = new FileSystemWatcher(); 
    watcher.Path = @"\\yourserver\yourshare"; 
    watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
      | NotifyFilters.FileName | NotifyFilters.DirectoryName; 
    // Only watch text files. 
    watcher.Filter = "*.txt"; 

    // Add handler for errors. 
    watcher.Error += new ErrorEventHandler(OnError); 

    // Begin watching. 
    watcher.EnableRaisingEvents = true; 

    // Wait for the user to quit the program. 
    Console.WriteLine("Press \'q\' to quit the sample."); 
    while(Console.Read()!='q'); 
} 
private static void OnError(object source, ErrorEventArgs e) 
{ 
    // Show that an error has been detected. 
    Console.WriteLine("The FileSystemWatcher has detected an error"); 

    // Give more information if the error is due to an internal buffer overflow. 
    Type t = e.GetException().GetType(); 
    Console.WriteLine(("The file system watcher experienced an error of type: " + 
         t.ToString() + " with message=" + e.GetException().Message)); 
} 

發現如果您嘗試啓動這個控制檯應用程序,然後關閉您的網絡連接,你會看到Win32Exception一個sligtly改編的例子,但如果你再這樣做,沒有更多的錯誤事件將由正在運行的FileSystemWatcher看到