Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

c# - FileSystemWatcher with Samba on Linux

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:privedefFolder";

        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");
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...