24

I am writing a c# program to let me know when a file has been added or deleted. I run it on my Windows 7 machine and watch an FTP server on our network.

It works fine but will suddenly stop catching any events. I'm guessing that it might be losing connection to the server or there is a glitch in the network.

How can I handle this situation in the code. Is there some exception I can watch for and try to restart the FileSystemWatcher object.

Any suggestions and code samples would be appreciated.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Paul
  • 757
  • 3
  • 10
  • 38
  • So if 1kb of the file has been uploaded, how do you know the transmission is complete? I think you need to rethink your approach here. – leppie May 31 '11 at 09:19
  • Check out this question about exceptions when reading created files with the FileSystemWatcher http://stackoverflow.com/questions/699538/file-access-error-with-filesystemwatcher-when-multiple-files-are-added-to-a-direc – Oskar Kjellin May 31 '11 at 09:26
  • It works fine to capture when files are created or deleted. I just need to know how to recover from the lost connection or network glitch. Is there any type of exception thrown on the FileSystemWatcher object? – Paul May 31 '11 at 23:51
  • I think I know what I need to do now. Catch the exception that the fileSystemWatcher throws and then try to re-enable raising events on it when the server is accessible again. I don't understand where I would put the try/catch at. – Paul Jun 01 '11 at 05:39

3 Answers3

24

I needed to add an error handler for the FileSystemWatcher

fileSystemWatcher.Error += new ErrorEventHandler(OnError);

And then add this code:

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);
}

Here is how I reset the SystemFileWatcher object:

   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);
            }
        }

    }

I think this code should do what I want it to do.

Paul
  • 757
  • 3
  • 10
  • 38
  • If this does indeed fix the problem you should mark it as the answer. For what it's worth, what was the exception that was being thrown? – Chris Shain Jun 02 '11 at 01:33
  • Chris, Actually I am not concerned with what the exception is I just want to make sure the FileSystemWatcher object is restarted. – Paul Jun 02 '11 at 11:45
17

The previous answer does not fix it completely, I had to reset the watcher not just turn it on and off. I use filesystemwatcher on a window service

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);
        }
    }
}
Rikin Patel
  • 8,848
  • 7
  • 70
  • 78
Rene
  • 302
  • 2
  • 4
  • this seems to keep looping and re-creating the filesystemwatcher even if it is successful at some point. Shouldn't there be a break in the "success" loop? – Matt Apr 20 '16 at 20:37
  • @Matt It should break on 1) path exists (accessible) and b)EnableRaisingEvents == true. – Andreas Reiff Oct 13 '17 at 08:56
  • 1
    This assigns a new `FileSystemWatcher` to a local variable. The caller still has a reference to the old instance, which is now disposed. – StackOverthrow Oct 05 '20 at 17:25
0

You can create a method that initiates the FileSystemWatcher, and in case of an error, just restart it.

    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();
    }