8

I'm using a FileSystemWatcher on my C# application (running on Windows) in order to update in my app the files that I'm currently browsing. It's work well when I browse a local directory. I am notified when a file is renamed, deleted or added. But for example when I rename a file on the network drive the first time, the FileSystemWatcher notifies me of a rename action and then, when I rename the same file or another file, the FileSystemWatcher notifies me of an error :

the specified server cannot perform the requested operation.

Then the FileSystemWatcher not notify me about anything.

Sometimes I can rename twice before the FileSystemWatcher not notify me nothing...

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");
    }
Akhilleus
  • 305
  • 4
  • 11
  • You mention linux, do you mean your client code is also running on linux (i.e. on Mono) or just the server with the Samba share? – Isak Savo Jul 03 '12 at 07:28
  • No the client runs on Windows and the server on Linux (Ubuntu Server 10.04) – Akhilleus Jul 03 '12 at 07:35
  • And to add... your sure you have correct privalages on the share to perform those actions? The filesystemwatcher requires permissions outlined here http://msdn.microsoft.com/en-us/library/aa365465%28v=vs.85%29.aspx – Nico Jul 03 '12 at 08:07

3 Answers3

7

First of all, file system monitoring of remote shares is always going to be somewhat unreliable. You should not depend on your app getting all the events - in fact, I would suggest you provide a backup polling mechanism to check for changes that you may have missed. A refresh button in the GUI could be another option, depending on your app.

That said, your particular problem doesn't seem to be that uncommon. I googled around a bit and found these things:

My guess is that this is a problem with certain versions (or configuration) of Samba combined with Windows. Are there anything in the Samba logs on the linux server that could point you towards the cause of the problem?

As an immediate workaround, I suggest you try these things:

  • Add a polling mechanism that ensures that you do get the folder changes even if the FSW breaks down
  • When the FSW breaks, try to "restart" it by creating a new one. You may also want to check if EnableRaisingEvents is set to false when you get the error - maybe you can just set it to true to start receiving events again.
  • (Grasping for straws here) try playing around with the internal buffer size in case that's the problem (I doubt it, but it's worth a shot)
Community
  • 1
  • 1
Isak Savo
  • 34,957
  • 11
  • 60
  • 92
  • 1
    Thank you very much, actually I just need to be notified when a change occurs in the directory browsed by the user. My problem was mostly lost events after an error events. By creating a new `FileSystemWatcher` when it breaks, I am constantly notified of file system changes. – Akhilleus Jul 03 '12 at 09:39
0
        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 Window Service FileSystemWatcher based class to Monitor a Samba Shared Folder onChanges and used DifferenceEngine from CodeProject to check the different and copy over to Window Shared Folder Path if there is a changes. I also added a Timer to check every 10 seconds to handle when network is fail. There is a List Array To store the changes count. Added into List when File Changed event is Raised and remove the List when is success.

I have tested two HP Laptop on Window 7 Pro OS, working fine.

But Failed to working on other Window 7 Pro Laptop and also Window XP Pro SP3 Desktop. (We are on same Company Network/VLAN and Service Pack)

The failed is mean if i amended something in Samba Shared Folder, it will not Sync the Latest content to Window Share Path.

I also has added these

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

in the top of the coding, it seem does not work

0

For people recently having this kind of problem, quoting from my answer to this question:

We are yet to test that this is a suitable alternative in our situation, but it seems that Microsoft introduced PhysicalFileProvider some time ago.

It has a UsePollingFileWatcher property specifically for this kind of problem.

Then for the file watching, it seems you want the Watch method.

The documentation is currently showing it as not available on certain platforms or target frameworks, but in practice the NuGet package claims to be .NET Standard 2.0 compatible, so that should cover basically everything.

Adam Goodwin
  • 3,951
  • 5
  • 28
  • 33