FileSystemWatcher的与Linux上的Samba [英] FileSystemWatcher with Samba on Linux

查看:246
本文介绍了FileSystemWatcher的与Linux上的Samba的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 FileSystemWatcher的上,以我的应用程序来更新我正在浏览的文件我的C#应用​​程序(在Windows上运行)。
它工作得很好,当我浏览本地目录。当一个文件被重命名,删除或添加我通知。
,而例如,当我在网络上重命名文件驱动首次 FileSystemWatcher的通知我重命名操作,然后,当我重新命名相同的文件或其他文件,在 FileSystemWatcher的通知我一个错误的:



指定的服务器无法执行请求的操作



然后FileSystemWatcher的没有通知我任何事情。



有时候,我可以在FileSystemWatcher的前两次重命名不通知我什么...



下面是我的测试代码:

 静无效的主要(字串[] args)
{
FileSystemWatcher的守望者=新FileSystemWatcher的();
watcher.Path = @N:\prive\defFolder

watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;

watcher.Changed + =新FileSystemEventHandler(watcher_Changed);
watcher.Created + =新FileSystemEventHandler(watcher_Changed);
watcher.Deleted + =新FileSystemEventHandler(watcher_Changed);
watcher.Renamed + =新RenamedEventHandler(watcher_Renamed);
watcher.Error + =新ErrorEventHandler(watcher_Error);

watcher.EnableRaisingEvents = TRUE;

Console.Read();
watcher.Dispose();
}

静态无效watcher_Error(对象发件人,ErrorEventArgs E)
{
Console.WriteLine(错误:+ e.GetException()消息);
}

静态无效watcher_Renamed(对象发件人,RenamedEventArgs E)
{
Console.WriteLine(改名成功);
}

静态无效watcher_Changed(对象发件人,FileSystemEventArgs E)
{
Console.WriteLine(修改成功);
}


解决方案

首先,文件系统远程监控股票总是会有些不可靠的。你不应该依赖于你的应用程序让所有的事件 - 事实上,我建议你提供一个备份轮询机制来检查,你可能已经错过了变化。在GUI刷新按钮可能是另一种选择,这取决于你的应用程序。



这是说,您特定的问题似乎并没有是少见。我用Google搜索了一下周围,发现这些东西:





我的猜测是,这是使用Samba的某些版本(或配置)中的问题与Windows相结合。是否有在Linux服务器上的Samba日志任何可以为您指出了问题的原因



作为一个直接的解决办法,我建议你试试这些东西:<? / p>


  • 添加,它确保你得到即使FSW打破了

  • 文件夹改变轮询机制
  • 当FSW休息,尽量通过创建一个新的重新启动它。您可能还需要检查是否 EnableRaisingEvents 设置为当你得到的错误 - 也许你可以将它设置为true,重新开始接收事件。

  • (用于抓秸秆在这里)尝试使用的的情况下,内部缓冲区大小这就是问题所在(我怀疑这一点,但它是值得一试)


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");
    }

解决方案

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)

这篇关于FileSystemWatcher的与Linux上的Samba的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆