FileSystemWatcher停止捕获事件

问题描述:

我正在写一个c#程序让我知道何时添加或删除了一个文件。我在我的Windows 7机器上运行它并在我们的网络上观看FTP服务器。FileSystemWatcher停止捕获事件

它工作正常,但会突然停止捕捉任何事件。我猜测它可能会失去与服务器的连接,或者网络出现故障。

我该如何处理代码中的这种情况。是否有一些例外我可以观察并尝试重新启动FileSystemWatcher对象。

任何建议和代码示例将不胜感激。

+0

因此,如果该文件的1KB已经上传,你怎么知道的传输是否已经完成?我认为你需要在这里重新考虑你的方法。 – leppie 2011-05-31 09:19:17

+0

在使用FileSystemWatcher读取创建的文件时,查看有关异常的此问题http://*.com/questions/699538/file-access-error-with-filesystemwatcher-when-multiple-files-are-added-toa-a- direc – 2011-05-31 09:26:21

+0

当文件被创建或删除时,捕获工作正常。我只需要知道如何从丢失的连接或网络故障中恢复。 FileSystemWatcher对象上是否存在任何类型的异常? – Paul 2011-05-31 23:51:59

上一个答案没有完全解决它,我不得不重置观察者,而不仅仅是打开和关闭它。 我用FileSystemWatcher的一个窗口服务

void NotAccessibleError(FileSystemWatcher source, ErrorEventArgs e) 
{ 
    int iMaxAttempts = 120; 
    int iTimeOut = 30000; 
    int i = 0; 
    while ((!Directory.Exists(source.Path) || source.EnableRaisingEvents == false) && i < iMaxAttempts) 
    { 
     i += 1; 
     try 
     { 
      source.EnableRaisingEvents = false; 
      if (!Directory.Exists(source.Path)) 
      { 
       MyEventLog.WriteEntry("Directory Inaccessible " + source.Path + " at " + DateTime.Now.ToString("HH:mm:ss")); 
       System.Threading.Thread.Sleep(iTimeOut); 
      } 
      else 
      { 
       // ReInitialize the Component 
       source.Dispose(); 
       source = null; 
       source = new System.IO.FileSystemWatcher(); 
       ((System.ComponentModel.ISupportInitialize)(source)).BeginInit(); 
       source.EnableRaisingEvents = true; 
       source.Filter = "*.tif"; 
       source.Path = @"\\server\dir"; 
       source.NotifyFilter = System.IO.NotifyFilters.FileName; 
       source.Created += new System.IO.FileSystemEventHandler(fswCatchImages_Changed); 
       source.Renamed += new System.IO.RenamedEventHandler(fswCatchImages_Renamed); 
       source.Error += new ErrorEventHandler(OnError); 
       ((System.ComponentModel.ISupportInitialize)(source)).EndInit(); 
       MyEventLog.WriteEntry("Try to Restart RaisingEvents Watcher at " + DateTime.Now.ToString("HH:mm:ss")); 
      } 
     } 
     catch (Exception error) 
     { 
      MyEventLog.WriteEntry("Error trying Restart Service " + error.StackTrace + " at " + DateTime.Now.ToString("HH:mm:ss")); 
      source.EnableRaisingEvents = false; 
      System.Threading.Thread.Sleep(iTimeOut); 
     } 
    } 
} 
+0

这似乎保持循环并重新创建文件系统监视器,即使它在某些时候是成功的。在“成功”循环中不应该有中断吗? – Matt 2016-04-20 20:37:54

+0

@Matt它应该打破1)路径存在(可访问)和b)EnableRaisingEvents == true。 – 2017-10-13 08:56:23

我需要添加一个错误处理程序FileSystemWatcher的

fileSystemWatcher.Error += new ErrorEventHandler(OnError); 

然后添加以下代码:

private void OnError(object source, ErrorEventArgs e) 
{ 
    if (e.GetException().GetType() == typeof(InternalBufferOverflowException)) 
    { 
     txtResults.Text += "Error: File System Watcher internal buffer overflow at " + DateTime.Now + "\r\n"; 
    } 
    else 
    { 
     txtResults.Text += "Error: Watched directory not accessible at " + DateTime.Now + "\r\n"; 
    } 
    NotAccessibleError(fileSystemWatcher ,e); 
} 

这是我如何重置SystemFileWatcher对象:

static void NotAccessibleError(FileSystemWatcher source, ErrorEventArgs e) 
    { 
     source.EnableRaisingEvents = false; 
     int iMaxAttempts = 120; 
     int iTimeOut = 30000; 
     int i = 0; 
     while (source.EnableRaisingEvents == false && i < iMaxAttempts) 
     { 
      i += 1; 
      try 
      { 
       source.EnableRaisingEvents = true; 
      } 
      catch 
      { 
       source.EnableRaisingEvents = false; 
       System.Threading.Thread.Sleep(iTimeOut); 
      } 
     } 

    } 

我认为这段代码应该做我想做的事情。

+0

如果这确实解决了问题,您应该将其标记为答案。对于什么是值得的,抛出的异常是什么? – 2011-06-02 01:33:52

+0

Chris,其实我并不关心什么是异常,我只是想确保FileSystemWatcher对象重新启动。 – Paul 2011-06-02 11:45:50