C# FileSystemWatcher停止捕获事件

C# FileSystemWatcher停止捕获事件,c#,filesystemwatcher,C#,Filesystemwatcher,我正在编写一个c#程序,让我知道文件何时被添加或删除。我在我的Windows7机器上运行它,并在我们的网络上观看FTP服务器 它工作正常,但会突然停止捕捉任何事件。我猜它可能会失去与服务器的连接,或者网络出现故障 我如何在代码中处理这种情况。是否存在一些异常,我可以监视并尝试重新启动FileSystemWatcher对象 任何建议和代码示例都将不胜感激。我需要为FileSystemWatcher添加一个错误处理程序 fileSystemWatcher.Error += new ErrorEven

我正在编写一个c#程序,让我知道文件何时被添加或删除。我在我的Windows7机器上运行它,并在我们的网络上观看FTP服务器

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

我如何在代码中处理这种情况。是否存在一些异常,我可以监视并尝试重新启动FileSystemWatcher对象


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

我需要为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);
            }
        }

    }
static void NotAccessibleError(FileSystemWatcher源代码,ErrorEventArgs e)
{
source.EnableRaisingEvents=false;
int imaxattens=120;
int iTimeOut=30000;
int i=0;
while(source.EnableRaisingEvents==false&&i

我认为这段代码应该做我希望它做的事情。

前面的答案并不能完全修复它,我必须重置观察程序,而不仅仅是打开和关闭它。 我在窗口服务上使用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);
        }
    }
}
void NotAccessibleError(FileSystemWatcher源代码,ErrorEventArgs e)
{
int imaxattens=120;
int iTimeOut=30000;
int i=0;
而((!Directory.Exists(source.Path)| | source.EnableRaisingEvents==false)&&i
您可以创建一个启动FileSystemWatcher的方法,如果出现错误,只需重新启动它

    private void WatchFile()
    {
        try
        {
            fsw = new FileSystemWatcher(path, filter)
            {
                EnableRaisingEvents = true
            };                
            fsw.Changed += Fsw_Changed;                
            fsw.Error += Fsw_Error;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    private void Fsw_Error(object sender, ErrorEventArgs e)
    {
        Thread.Sleep(1000);
        fsw.Changed -= Fsw_Changed;
        fsw.Error -= Fsw_Error;
        WatchFile();
    }

因此,如果上传了1kb的文件,您如何知道传输已完成?我认为您需要重新考虑您的方法。请查看这个关于使用FileSystemWatcher读取创建的文件时出现异常的问题。在创建或删除文件时捕获异常效果很好。我只需要知道如何从丢失的连接或网络故障中恢复。FileSystemWatcher对象上是否存在任何类型的异常?我想我知道现在需要做什么了。捕获fileSystemWatcher引发的异常,然后在服务器再次可访问时尝试重新启用引发事件。我不明白我该把try/catch放在哪里。如果这确实解决了问题,你应该把它标记为答案。值得一提的是,抛出的异常是什么?Chris,实际上我不关心异常是什么,我只是想确保FileSystemWatcher对象重新启动。这似乎会继续循环并重新创建FileSystemWatcher,即使它在某个点上成功。“成功”循环中不应该有中断吗?@Matt它应该在1)路径存在(可访问)和b)EnableRaisingEvents==true上中断。这将为局部变量分配一个新的
FileSystemWatcher
。调用方仍然有一个对旧实例的引用,该引用现在已被释放。