FileSystemWatcher with Samba on Linux

I am using FileSystemWatcher in my C # application (runs on Windows) to update the files that I am currently viewing in my application. It works well when I browse the local directory. I get a notification when a file is renamed, deleted or added. But, for example, when I first renamed a file on a network drive, FileSystemWatcher notifies me of the renaming action, and then when I rename the same file or another file, FileSystemWatcher notifies me of an error:

the specified server cannot perform the requested operation .

Then FileSystemWatcher doesn't report anything about me.

Sometimes I can rename twice before FileSystemWatcher tells me anything ...

Here is my test code:

  static void Main(string[] args) { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = @"N:\prive\defFolder"; watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite; watcher.Changed += new FileSystemEventHandler(watcher_Changed); watcher.Created += new FileSystemEventHandler(watcher_Changed); watcher.Deleted += new FileSystemEventHandler(watcher_Changed); watcher.Renamed += new RenamedEventHandler(watcher_Renamed); watcher.Error += new ErrorEventHandler(watcher_Error); watcher.EnableRaisingEvents = true; Console.Read(); watcher.Dispose(); } static void watcher_Error(object sender, ErrorEventArgs e) { Console.WriteLine("error : " + e.GetException().Message); } static void watcher_Renamed(object sender, RenamedEventArgs e) { Console.WriteLine("rename success"); } static void watcher_Changed(object sender, FileSystemEventArgs e) { Console.WriteLine("change success"); } 
+4
source share
2 answers

First of all, monitoring the file system of remote shares will always be somewhat unreliable. You should not depend on the fact that your application receives all the events - in fact, I would suggest you provide a backup mechanism to check for changes that you might have missed. The refresh button in the GUI may be another option, depending on your application.

However, your specific problem does not seem so unusual. I ruined a little and found these things:

I assume this is a problem with some versions (or configuration) of Samba in conjunction with Windows. Is there anything in the Samba logs on the Linux server that may indicate the cause of the problem?

As an immediate workaround, I suggest you try the following:

  • Add a polling mechanism that ensures that you get folder changes even if FSW breaks
  • When the FSW breaks, try restarting it by creating a new one. You can also check if EnableRaisingEvents is EnableRaisingEvents to false on error: maybe you can just set it to true to start receiving events again.
  • (Grasping for straws here) try playing with the size of the internal buffer in case there is a problem (I doubt it is worth a shot)
+4
source
  if (Directory.Exists(monitorPath)) { watcher.Path = monitorPath; watcher.IncludeSubdirectories = true; watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.CreationTime; watcher.InternalBufferSize = 65536; watcher.Filter = "test_prod-Pg1_ICT*"; watcher.Changed += new FileSystemEventHandler(fileChangedEvent); watcher.EnableRaisingEvents = true; LogEvent("Folder Syncronization Service is Started!"); } 

I created a Windows Service FileSystemWatcher-based class to monitor the Samba onChanges shared folder and used the DifferenceEngine from CodeProject to check different versions and copy them to the shared path to the Windows folder if there are changes. I also added a timer to check every 10 seconds when the network fails. There is an array of lists to keep track of changes. Added to the list when an event with a modified file is added and the List is deleted when successful.

I tested two HP laptops for laptops on Windows 7 Pro, working fine.

But it was not possible to work with another Window 7 Pro laptop, as well as with Windows XP Pro SP3 Desktop. (We are in the same company network / VLAN and Service Pack)

Failure means that if I change something in the Samba Shared Folder, it will not sync the latest content with the Windows Share Path.

I also added these

[PermissionSetAttribute (SecurityAction.LinkDemand, Name = "FullTrust")] [PermissionSetAttribute (SecurityAction.InheritanceDemand, Name = "FullTrust")]

at the top of the encoding does not seem to work

0
source

All Articles