2012-12-18 18 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看到