【问题标题】:FileSystemWatcher and network disconnect?FileSystemWatcher 和网络断开连接?
【发布时间】:2008-11-11 17:30:42
【问题描述】:

如何使它在网络中工作?它可以工作,然后无缘无故停止工作(可能是因为网络不完美)。

【问题讨论】:

    标签: c# .net .net-2.0 filesystemwatcher


    【解决方案1】:

    您需要重新连接 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.
            }
         }
      }
    

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

    【讨论】:

    • 此代码不可靠,因为 FileSystemWatcher 在监视网络文件夹或驱动器时并不总是触发错误事件。
    • 当网络关闭时,会显示错误。什么情况下不靠谱?
    • 如果 FileSystemWatcher 用于监视网络文件夹,如果网络出现问题,FileSystemWatcher.Error 并不总是被触发。这是已知问题,您的代码将不会被执行。
    • 你有什么参考吗?因为我的代码多年来一直在使用该代码,而且效果很好。另外,你有什么选择吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-27
    • 1970-01-01
    相关资源
    最近更新 更多