简体   繁体   中英

Configure settings of .NET Framework 4.5's System.IO.FileSystemWatcher to be Communicative about Errors, Fault-tolerant, Robust, intelligent, etc

At my office, we are using ( https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.5 ) .NET Framework 4.5's System.IO.FileSystemWatcher

We have a application modules running in a distributed network environment.

One of the remote application module will have an instance of System.IO.FileSystemWatcher watching for files within a network shared directory.

In some cases, from the perspective of said remote application , the network shared directory may go offline, and then come back up after a few minutes.

We the following C# code snippet within our remote application module's code:

FileSystemWatcher    _watcher = new FileSystemWatcher
    {
        Path = DirectoryPath,
        NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                                                | NotifyFilters.FileName | NotifyFilters.DirectoryName,
        Filter = FileNameFilter
    };

    _watcher.Created += Watcher_CreatedChanged;
    _watcher.EnableRaisingEvents = true;

We noticed that if the network shared directory may goes offline temporarily, and then comes back up after few minutes then Our remote application module's FileSystemWatcher instance

-does Not throw an error,

-and what's even worse is that it does Not notice files that may have been dropped into said network shared directory while the network shared directory was offline from the perspective of the remote application module

Could someone please tell me what modifications/additions do I have to make to the aforementioned configuration code in order to make the FileSystemWatcher instance

-more communicative about errors?

-more robust and intelligent about recovering when the network shared directory comes back online? ( which in our case would mean noticing the files that were placed in the network shared directory while it went offline(from the perspective of our remote application module )

-more fault tolerant?

The FileSystemWatcher has an Error event, are you handling that? Your snippet shows that you're handling the Created event, but not any others.

The following stackoverflow.com Q & A posting was helpful:

FileSystemWatcher vs polling to watch for file changes

@peter-meinl However, I really want to Thank @peter-meinl for the following posting on Wordpress.com which was very helpful because he created these feature-rich "wrapper" classes for the .NET's standard FileSystemWatcher

https://petermeinl.wordpress.com/2015/05/18/tamed-filesystemwatcher/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM