2008-11-11 40 views

回答

7

您需要重新连接FileSystemWatcher。

让你的类型FileSystemWatcher的全局的变量类,添加事件WatcherError。

里面的方法,加上类似的东西:

private static void WatcherError(object source, ErrorEventArgs e) 
    { 
    watcher = new FileSystemWatcher();//You might want to do a method and to setup all config... 
    while (!watcher.EnableRaisingEvents) 
    { 
     try 
     { 
      watcher = new FileSystemWatcher();//You might want to do a method and to setup all config... 
     } 
     catch 
     { 
      System.Threading.Thread.Sleep(30000); //Wait for retry 30 sec. 
     } 
    } 
    } 

你不想使用观察家=新......你希望有这将增加所有事件,并设置路径的方法,但上面的代码可以帮助您了解要做什么。

+0

此代码不可靠,因为在监视网络文件夹或驱动器时,FileSystemWatcher不会始终启动错误事件。 – Tomas 2011-10-19 08:15:36