C# FileSystemWatcher Serious Problem?

前端 未结 4 1209
清歌不尽
清歌不尽 2021-01-21 03:42

I have implemented a FileSystemWatcher for a folder which resides on a NetworkStorage Device (having no O.S.).

The scenario is, we have two machine say

相关标签:
4条回答
  • 2021-01-21 04:06

    We built a product for a company where a Windows service running on a server was monitoring a folder and when files were added to this folder, the files were read, processed (in this case, created a barcode layout and printed on a barcode printer) and then deleted.

    Everything worked perfectly for a number of customers with pretty good performance until we came across a customer where it only worked sometimes. There were especially problems when many files were added to the folder at once.

    The problem were that the folder that we were watching was in a share on a samba filesystem and FileSystemWatcher does not work reliably on samba file system shares (Google for "FileSystemwatcher samba"). Since you are talking about a "NetworkStorage Device", I guess that you mean that it is a NAS, and NAS quite often use a Linux/Unix OS under the hoods and expose share using samba.

    Our solution were to add a feature to our software so that it could be configured to use polling when needed.

    0 讨论(0)
  • 2021-01-21 04:18

    FileSystemWatcher vs polling to watch for file changes

    See the first answer. File system watchers are not reliable, esp. over network.

    Doesn't appear to address your issue, since you seem to be getting events, is this maybe a timing issue?

    0 讨论(0)
  • 2021-01-21 04:21

    FileSystemWatcher relies on the Operating System to Raise the Event. If there is no OS on the Storage Device there is no OS available to Raise the Event.

    Note that several factors can affect which file system change events are raised, as described by the following:

    Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

    The FileSystemWatcher can watch disks as long as they are not switched or removed. The FileSystemWatcher does not raise events for CDs and DVDs, because time stamps and properties cannot change. Remote computers must have one of the required platforms installed for the component to function properly.

    If multiple FileSystemWatcher objects are watching the same UNC path in Windows XP prior to Service Pack 1, or Windows 2000 SP2 or earlier, then only one of the objects will raise an event. On machines running Windows XP SP1 and newer, Windows 2000 SP3 or newer or Windows Server 2003, all FileSystemWatcher objects will raise the appropriate events.

    Setting the Filter property does not decrease what goes into the buffer.

    Note that a FileSystemWatcher does not raise an Error event when an event is missed or when the buffer size is exceeded, due to dependencies with the Windows operating system. To keep from missing events, follow these guidelines:

    Increasing the buffer size with the InternalBufferSize property can prevent missing file system change events.

    Avoid watching files with long file names. Consider renaming using shorter names.

    Keep your event handling code as short as possible.

    Source: MSDN Library Reference: FileSystemWatcher Class

    0 讨论(0)
  • 2021-01-21 04:21

    Use a combination of a FileSystemWatcher and Polling

    The FileSystemWatcher will work 90% of the time, but add a poll every x minutes to catch anything it may miss...

    The value of x depends on how "realtime" you need to be

    0 讨论(0)
提交回复
热议问题