FileSystemWatcher和网络断开?

Mis*_*Dev 6 .net c# filesystemwatcher .net-2.0

如何让它在网络中运行?它工作然后它没有理由停止工作(可能因为网络不完美).

Pat*_*ins 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.
        }
     }
  }
Run Code Online (Sandbox Code Playgroud)

你不想使用watcher = new ...你希望有一个方法可以添加所有事件并设置路径,但上面的代码可以让你很好地了解该怎么做.