Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# FileSystemWatcher Serious Problem?

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 says machine A and machine B. my application is having two buttons on my form Button1 and Button2. I have written code for renaming the Test.txt file.

Button1: renaming the file Test.txt to Test007.txt and Button2: renaming the file Test007.txt to Test.txt

And suppose I have run tis exe file from A and B simultaneously. Then I am able to rename file by pressing Button1 from Machine A and now If I am trying to rename the file from the another Machine B then it is giving an error

"System.ComponentModel.Win32Exception: The specified server can not perform the requested operation"

Anybody is having any Idea about it...please help us to find the solution. We are having proper rights for the folder.

Note : Code is also working for LAN Folder, So the code is perfect.it is just not working for NetworkStoreDevice.

like image 382
Pradip Avatar asked Dec 29 '22 07:12

Pradip


2 Answers

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.

like image 79
Andreas Paulsson Avatar answered Jan 05 '23 01:01

Andreas Paulsson


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

like image 45
cory-fowler Avatar answered Jan 04 '23 23:01

cory-fowler