0

I have a FileSystemWatcher that runs on the network folder, and it worked for a week without any issue after I deployed it into the server. After that, FSW doesn't work, and I have to re-deploy to make it work. I am not sure exactly what I am missing here. Thanks for the suggestions in advance.

private readonly FileSystemWatcher _watcher = new FileSystemWatcher();


public void Start()
{
        _watcher.Path = "Network Shared folder Path";
        _watcher.Filter = "*.*";
        _watcher.NotifyFilter =
            NotifyFilters.LastAccess |
            NotifyFilters.LastWrite |
            NotifyFilters.FileName |
            NotifyFilters.DirectoryName;

        _watcher.EnableRaisingEvents = true;

        _watcher.Created += async (o, e) =>
        {
            
            try
            {
               // Busineess logic to read the file
            }
            catch (Exception exception)
            {
            }
        };

    }
}

public void Stop()
{
    _watcher.EnableRaisingEvents = false;
    _watcher.Dispose();
}
Markus
  • 20,838
  • 4
  • 31
  • 55
Bhargav
  • 97
  • 1
  • 1
  • 9
  • 1
    How are Start and Stop called? Please give debugging details, e.g. a [mcve]. – JHBonarius Jul 18 '22 at 14:23
  • 2
    Directory change notifications (which are what `FileSystemWatcher` wraps) are notoriously flaky when it comes to network resources, as in, the notification breaking is not always signaled. Running for a week without trouble is actually pretty decent. You may want to consider periodically restarting the watch, updating internal state as you go along. – Jeroen Mostert Jul 18 '22 at 14:28
  • @JHBonarius I use the console app and Topshelf package to run the application as a window service. I can't reproduce this issue in my local because this code works for one or two weeks without any issue in the server. – Bhargav Jul 18 '22 at 14:30
  • 1
    Do you have proper logging added to your application? Without that guessing is all you/we can do. Implement some sort of logging especially for the Error Event of the FileSystemWatcher. Then you might get clues what is happening. Presumably some short disconnects of the share that end in a permanently disconnected FileSystemWatcher. In that case consider reconnecting then as Jeroen already suggests. – Ralf Jul 18 '22 at 14:39
  • @Ralf I use log4net for logging. Unfortunately, I couldn't find any specific logs. But I will try by adding "_watcher. Error" in the code. – Bhargav Jul 18 '22 at 14:45

0 Answers0